@mengine/medeo-client 2.0.1-alpha.4 → 2.0.1-alpha.6

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.
@@ -0,0 +1,4589 @@
1
+ import { u as volumeSchema } from "./shared-iWnU2osE.js";
2
+ import { Mirror, schema } from "loro-mirror";
3
+ import { z } from "zod";
4
+ import { LoroDoc, LoroMap, LoroMovableList, LoroText, UndoManager } from "loro-crdt";
5
+ import { sha256 } from "@noble/hashes/sha2.js";
6
+ import { bytesToHex } from "@noble/hashes/utils.js";
7
+ import { produce } from "immer";
8
+ //#region src/client/base64.ts
9
+ function bytesToBase64(bytes) {
10
+ let binary = "";
11
+ for (const byte of bytes) binary += String.fromCharCode(byte);
12
+ return btoa(binary);
13
+ }
14
+ function base64ToBytes(base64) {
15
+ const binary = atob(base64);
16
+ const bytes = new Uint8Array(binary.length);
17
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
18
+ return bytes;
19
+ }
20
+ //#endregion
21
+ //#region src/document/mirror-schema.ts
22
+ /**
23
+ * Declarative `loro-mirror` schema for `VideoDocument` — the canonical structural
24
+ * SSOT and live storage wire (replaces the hand-rolled `@mengine/schema`
25
+ * definition + adapter, see ADR 0008).
26
+ *
27
+ * Mirror gives an in-memory immutable state synced to the Loro doc by declarative
28
+ * diff, so the storage layer no longer hand-writes per-region reconcile or a
29
+ * transaction state machine. The shapes here store only authoritative facts
30
+ * (RFC 02 §6): per-item absolute time, `part_aggregations`, and total duration
31
+ * are projection-derived (cascade-solved at read time), so they are absent here.
32
+ *
33
+ * - tracks live in a single `tracks` `LoroMovableList` keyed by track id
34
+ * (reference/17 §4). Lane is expressed by each track's `parts_kind`, not by
35
+ * which container it lives in; order is the movable-list order itself. There is
36
+ * no `lane` / `lane_order` field, so two peers inserting tracks never collide
37
+ * on an order integer — list moves are conflict-free (RFC 03 §4). The legacy
38
+ * keyed-map (`LoroMapRecord` + an integer `lane_order`) and the prior three
39
+ * named containers are gone; the `main` / `above` / `below` three-pane view is
40
+ * rebuilt by the projection from `parts_kind`.
41
+ * - each track's `items` is a `LoroMovableList` keyed by `part_id`: a part is
42
+ * placed at most once per lane, so `part_id` is the stable placement identity.
43
+ * Reorder diffs to a real Loro `move`, preserving per-item CRDT identity on
44
+ * every lane (main and secondary alike).
45
+ * - `time_position` is the authoritative positioning fact (RFC 02 §4,
46
+ * reference/17 §3), carried as an opaque JSON blob string (a whole tagged-union
47
+ * value, last-writer-wins). The derived `fallback_abs_ms` snapshot sits beside
48
+ * it as its own number field — a separate LWW unit so a projection refresh
49
+ * never clobbers a `time_position` edit.
50
+ * - meta lives in a `meta` LoroMap: the mirror root only accepts container
51
+ * schemas, so meta scalars can't sit at the root as bare values. The domain
52
+ * `VideoDocument` keeps the same `meta` wrapper, so the shapes are isomorphic
53
+ * and the read/seed mapping is a near-identity.
54
+ * - `part_library` values are nested `LoroMap`s: the value is a `LoroMap` with
55
+ * four mutually-exclusive optional part sub-maps (`video_clip` / `speech` /
56
+ * `caption` / `bgm`), each a `LoroMap` whose fields are stored as real Loro
57
+ * sub-keys. This makes each field its own CRDT unit, so two peers editing
58
+ * different fields of the same part (e.g. one volume, one play_out) merge
59
+ * field-by-field instead of one whole-value overwrite. Field `required` mirrors
60
+ * the Smithy `@required` contract (see `video_draft_comp.smithy`). `kind` is not
61
+ * stored (the present sub-key names the kind); `duration_ms` is not stored (it
62
+ * is derived, reference/17 §5). Nested values that the engine does not edit
63
+ * field-by-field — `speed_shift` (a tagged union), `voice`, `caption_ids` — stay
64
+ * as opaque JSON-blob sub-keys via `transform`. The four sub-keys are optional,
65
+ * so the "exactly one part kind" union invariant is not enforced by the schema
66
+ * type; it is rebuilt by `draftToPartUnion` on read and gated by zod on write.
67
+ * - a caption's `style` is the one lazily-created optional child container, so
68
+ * `captionPart` sets `mergeableMapChildContainers: true` — two peers can
69
+ * concurrently first-create the same caption's `style` (one attribute each), and
70
+ * only a mergeable child converges instead of last-writer-wins dropping one. All
71
+ * other nested maps are keyed by a unique id (`part_library`) or created
72
+ * atomically with their parent (`partValue`'s part sub-map), so they never hit
73
+ * that concurrent-first-create fork and stay on plain `setContainer`. See the
74
+ * note on `captionPart` below.
75
+ * - `video_creation_settings` is an opaque JSON blob carried in a string via
76
+ * `transform`: a whole value, last-writer-wins (no field-level concurrent edits).
77
+ */
78
+ /**
79
+ * JSON-blob transform for an opaque, last-writer-wins value carried in a Loro
80
+ * string. The field must be declared `required: false`: an absent field decodes
81
+ * to `undefined` (mirror never calls `decode`/`encode` for null/undefined),
82
+ * which is how we represent "no value" instead of encoding a `null` sentinel.
83
+ */
84
+ function jsonTransform() {
85
+ return {
86
+ decode: (value) => JSON.parse(value),
87
+ encode: (value) => JSON.stringify(value),
88
+ isEqual: "encoded-value-equality"
89
+ };
90
+ }
91
+ const trackItem = schema.LoroMap({
92
+ part_id: schema.String(),
93
+ time_position: schema.String().transform(jsonTransform()),
94
+ fallback_abs_ms: schema.Number({ required: false })
95
+ });
96
+ const track = schema.LoroMap({
97
+ id: schema.String(),
98
+ parts_kind: schema.String({ required: false }),
99
+ is_hidden: schema.Boolean({ required: false }),
100
+ items: schema.LoroMovableList(trackItem, (item) => item.part_id)
101
+ });
102
+ const videoClipPart = schema.LoroMap({
103
+ id: schema.String(),
104
+ play_in: schema.Number(),
105
+ play_out: schema.Number(),
106
+ volume: schema.Number(),
107
+ origin_media_id: schema.String(),
108
+ speed_shift: schema.String({ required: false }).transform(jsonTransform())
109
+ }, { required: false });
110
+ const speechPart = schema.LoroMap({
111
+ id: schema.String(),
112
+ media_duration_ms: schema.Number(),
113
+ audio_script: schema.String(),
114
+ volume: schema.Number(),
115
+ audio_storage_key: schema.String(),
116
+ origin_speech_id: schema.String(),
117
+ voice: schema.String().transform(jsonTransform()),
118
+ caption_ids: schema.String().transform(jsonTransform())
119
+ }, { required: false });
120
+ const captionStyle$1 = schema.LoroMap({
121
+ font_id: schema.String({ required: false }),
122
+ font_size: schema.Number({ required: false }),
123
+ font_color: schema.String({ required: false }),
124
+ font_weight: schema.Number({ required: false }),
125
+ entrance_animation: schema.String({ required: false }),
126
+ entrance_animation_duration_ms: schema.Number({ required: false }),
127
+ stroke_color: schema.String({ required: false }),
128
+ stroke_width: schema.Number({ required: false }),
129
+ position_x: schema.Number({ required: false }),
130
+ position_y: schema.Number({ required: false })
131
+ }, { required: false });
132
+ const captionPart = schema.LoroMap({
133
+ display_cues: schema.String({ required: false }).transform(jsonTransform()),
134
+ id: schema.String(),
135
+ initial_duration_ms: schema.Number(),
136
+ speech_part_id: schema.String({ required: false }),
137
+ text: schema.String(),
138
+ start_ms: schema.Number(),
139
+ style: captionStyle$1
140
+ }, {
141
+ required: false,
142
+ mergeableMapChildContainers: true
143
+ });
144
+ const bgmPart = schema.LoroMap({
145
+ id: schema.String(),
146
+ audio_storage_key: schema.String(),
147
+ volume: schema.Number(),
148
+ origin_media_id: schema.String()
149
+ }, { required: false });
150
+ const partValue = schema.LoroMap({
151
+ video_clip: videoClipPart,
152
+ speech: speechPart,
153
+ caption: captionPart,
154
+ bgm: bgmPart
155
+ });
156
+ const videoDocumentMirrorSchema = schema({
157
+ meta: schema.LoroMap({
158
+ schema_version: schema.String({ required: false }),
159
+ draft_id: schema.String({ required: false }),
160
+ project_id: schema.String({ required: false }),
161
+ owner_id: schema.String({ required: false }),
162
+ thumbnail_storage_key: schema.String({ required: false }),
163
+ chat_session_id: schema.String({ required: false }),
164
+ video_creation_settings: schema.String({ required: false }).transform(jsonTransform()),
165
+ version: schema.Number({ required: false })
166
+ }),
167
+ timeline: schema.LoroMap({ unit_time_ms: schema.Number({ required: false }) }),
168
+ tracks: schema.LoroMovableList(track, (t) => t.id),
169
+ part_library: schema.LoroMapRecord(partValue)
170
+ });
171
+ /** Project a discriminated `PartUnion` into the draft's four-optional shape. */
172
+ function partUnionToDraft(part) {
173
+ if (part.video_clip != null) return { video_clip: omitKind(part.video_clip) };
174
+ if (part.speech != null) return { speech: omitKind(part.speech) };
175
+ if (part.caption != null) return { caption: omitKind(part.caption) };
176
+ return { bgm: omitKind(part.bgm) };
177
+ }
178
+ /**
179
+ * Rebuild a discriminated `PartUnion` from a draft part value: pick the one
180
+ * present sub-key, restore its `kind`, and drop the `$cid`. Returns `undefined`
181
+ * when no part sub-key is present (an empty/placeholder value).
182
+ */
183
+ function draftToPartUnion(value) {
184
+ if (value == null) return void 0;
185
+ const v = value;
186
+ if (v.video_clip != null) return { video_clip: withKind(v.video_clip, "video_clip") };
187
+ if (v.speech != null) return { speech: withKind(v.speech, "speech") };
188
+ if (v.caption != null) return { caption: withKind(v.caption, "caption") };
189
+ if (v.bgm != null) return { bgm: withKind(v.bgm, "bgm") };
190
+ }
191
+ /** Drop `kind` (not stored) and `$cid` from a part payload for the draft. */
192
+ function omitKind(part) {
193
+ const { kind: _kind, $cid: _cid, ...rest } = part;
194
+ return rest;
195
+ }
196
+ /** Restore `kind` (and drop `$cid`) on a part sub-map read from the draft. */
197
+ function withKind(part, kind) {
198
+ const { $cid: _cid, ...rest } = part;
199
+ return {
200
+ ...rest,
201
+ kind
202
+ };
203
+ }
204
+ /** Entries of a draft record (e.g. `tracks`) with `$cid` stripped. */
205
+ function recordEntries(record) {
206
+ if (record == null) return [];
207
+ const out = [];
208
+ for (const [key, value] of Object.entries(record)) {
209
+ if (key === "$cid") continue;
210
+ out.push([key, value]);
211
+ }
212
+ return out;
213
+ }
214
+ //#endregion
215
+ //#region src/document/types.ts
216
+ const VIDEO_DOCUMENT_SCHEMA_VERSION = "video-document/v0";
217
+ /** A server-derived compatibility view, never an editor write authority. */
218
+ const ENTITY_TIMELINE_PROJECTION_SCHEMA_VERSION = "video-document/entity-projection-v1";
219
+ /** The linear speed multiplier of a `speed_shift`, defaulting to 1 (original). */
220
+ function speedOf(speedShift) {
221
+ const speed = speedShift?.config?.linear?.speed;
222
+ return typeof speed === "number" && Number.isFinite(speed) && speed > 0 ? speed : 1;
223
+ }
224
+ /**
225
+ * A video clip's effective timeline duration, derived from authoritative facts
226
+ * (RFC 02, `reference/16` §4, reference/17 §5): the trim window
227
+ * `play_out - play_in` divided by the speed multiplier, rounded to integer ms.
228
+ * `play_in` / `play_out` are optional in the IDL but every write path sets them
229
+ * (defaulting to the whole media), and the legacy-ingest projection backfills the
230
+ * window from a legacy `duration_ms` — so an authoritative clip always carries a
231
+ * trim window and there is no stored `duration_ms` to fall back to. A clip with
232
+ * neither bound yields 0. Speeds the clip up (>1× → shorter) or down (<1×).
233
+ */
234
+ function effectiveVideoClipDurationMs(clip) {
235
+ const speed = speedOf(clip.speed_shift);
236
+ const sourceMs = (clip.play_out ?? 0) - (clip.play_in ?? 0);
237
+ const effective = Math.round(sourceMs / speed);
238
+ return Number.isFinite(effective) && effective > 0 ? effective : 0;
239
+ }
240
+ /**
241
+ * The `timeline.unit_time_ms` the read-view states when the document states none.
242
+ *
243
+ * One frame at 30fps. A document legitimately has no such fact (display
244
+ * granularity is the editor's, not the timeline's), but the read-view IDL marks
245
+ * the field `@required`, so the projection has to name a value. This one is a
246
+ * usable granularity; `0` — which a downstream consumer had been defaulting to —
247
+ * is not, and passes any `>= 0` guard on its way to an editor that cannot use it.
248
+ */
249
+ const DEFAULT_UNIT_TIME_MS = 33.333;
250
+ //#endregion
251
+ //#region src/timeline-core/types.ts
252
+ /** Total document duration when there is no real content (matches FE bgm fallback). */
253
+ const TIMELINE_SKELETON_DURATION_MS = 2e4;
254
+ /** Empty placeholder clip marker: a video_clip part with no backing media. */
255
+ function isEmptyVideoClip(part) {
256
+ const clip = part?.video_clip;
257
+ if (clip == null) return false;
258
+ return clip.origin_media_id == null || clip.origin_media_id === "";
259
+ }
260
+ /** Clamp a duration to a non-negative integer (NaN/Infinity/negative → 0). */
261
+ function safeDurationMs(value) {
262
+ const n = Number(value);
263
+ if (!Number.isFinite(n) || n <= 0) return 0;
264
+ return Math.round(n);
265
+ }
266
+ function partDurationMs(doc, partId) {
267
+ const part = doc.part_library[partId];
268
+ if (part?.video_clip != null) return safeDurationMs(effectiveVideoClipDurationMs(part.video_clip));
269
+ if (part?.bgm != null) return doc.timeline.duration_ms > 0 ? doc.timeline.duration_ms : TIMELINE_SKELETON_DURATION_MS;
270
+ if (part?.speech != null) return safeDurationMs(part.speech.media_duration_ms);
271
+ if (part?.caption != null) return safeDurationMs(part.caption.initial_duration_ms);
272
+ return 0;
273
+ }
274
+ //#endregion
275
+ //#region src/timeline-core/cascade.ts
276
+ /**
277
+ * Canonical timeline cascade primitives (ADR 0009).
278
+ *
279
+ * Single source of truth for "how an edit's connected regions move": main-track
280
+ * seamless layout, aggregation position sync + reassignment, total-duration
281
+ * recompute, speech-overlap resolution, gap filling. Reconciled per ADR 0009 §4:
282
+ *
283
+ * - product behavior follows the FE current implementation;
284
+ * - all times are integer ms — positions/durations are rounded, never floated;
285
+ * - function decomposition follows agent-harness (the Python-derived structure).
286
+ *
287
+ * Every function mutates the `TimelineDoc` in place (the editor runs them inside
288
+ * one immer `transact`, so in-place edits diff correctly).
289
+ */
290
+ /**
291
+ * Lay the main track out head-to-tail from 0, rewriting each item's
292
+ * `abs_time_position`. Items whose part is missing from the library are dropped.
293
+ */
294
+ function arrangeMainTrackSeamlessly(doc) {
295
+ const kept = [];
296
+ let cursor = 0;
297
+ for (const item of doc.main_track) {
298
+ if (doc.part_library[item.part_id] == null) continue;
299
+ item.abs_time_position = cursor;
300
+ cursor += partDurationMs(doc, item.part_id);
301
+ kept.push(item);
302
+ }
303
+ doc.main_track = kept;
304
+ }
305
+ /**
306
+ * Sync attached parts' absolute positions from their host:
307
+ * speech.abs = host_video.abs + relative_time_position
308
+ * caption.abs = speech.abs + caption.start_ms
309
+ */
310
+ function syncAggregatedClipsTimePosition(doc) {
311
+ const videoByPart = indexByPart(doc.main_track);
312
+ const speechByPart = indexByPart(doc.speech_track);
313
+ for (const aggregation of doc.aggregations) {
314
+ const videoItem = videoByPart.get(aggregation.body_part_id);
315
+ if (videoItem == null) continue;
316
+ for (const attachment of aggregation.attachments) {
317
+ const speechItem = speechByPart.get(attachment.part_id);
318
+ if (speechItem == null) continue;
319
+ speechItem.abs_time_position = videoItem.abs_time_position + attachment.relative_time_position;
320
+ }
321
+ }
322
+ for (const captionItem of doc.caption_track) {
323
+ const captionPart = doc.part_library[captionItem.part_id]?.caption;
324
+ if (captionPart == null) continue;
325
+ const speechItem = captionPart.speech_part_id == null ? void 0 : speechByPart.get(captionPart.speech_part_id);
326
+ if (speechItem == null) continue;
327
+ captionItem.abs_time_position = speechItem.abs_time_position + safeDurationMs(captionPart.start_ms);
328
+ }
329
+ }
330
+ /**
331
+ * Reassign each speech to the video clip whose time range contains its start,
332
+ * rebuilding `aggregations`. A speech before the first clip or after the last
333
+ * falls back to the first / last clip respectively (FE: see §4 note — FE falls
334
+ * back to last only; we keep the harness two-sided fallback because a speech
335
+ * dragged before clip 0 belonging to the last clip is clearly wrong, and the FE
336
+ * single-sided rule is an acknowledged rough edge). `relative_time_position` is
337
+ * clamped to a non-negative integer.
338
+ */
339
+ function reassignSpeechesToVideoClipsByTime(doc) {
340
+ const ranges = doc.main_track.filter((item) => doc.part_library[item.part_id] != null).map((item) => ({
341
+ part_id: item.part_id,
342
+ start: item.abs_time_position,
343
+ end: item.abs_time_position + partDurationMs(doc, item.part_id)
344
+ }));
345
+ if (ranges.length === 0) {
346
+ doc.aggregations = [];
347
+ return;
348
+ }
349
+ const rebuilt = /* @__PURE__ */ new Map();
350
+ for (const speechItem of doc.speech_track) {
351
+ const start = speechItem.abs_time_position;
352
+ const targetRange = ranges.find((r) => r.start <= start && start < r.end) ?? (start < ranges[0].start ? ranges[0] : ranges[ranges.length - 1]);
353
+ const relative = Math.max(0, Math.round(start - targetRange.start));
354
+ const targetId = targetRange.part_id;
355
+ let aggregation = rebuilt.get(targetId);
356
+ if (aggregation == null) {
357
+ aggregation = {
358
+ body_part_id: targetId,
359
+ attachments: []
360
+ };
361
+ rebuilt.set(targetId, aggregation);
362
+ }
363
+ aggregation.attachments.push({
364
+ part_id: speechItem.part_id,
365
+ relative_time_position: relative
366
+ });
367
+ }
368
+ doc.aggregations = ranges.map((r) => rebuilt.get(r.part_id)).filter((a) => a != null);
369
+ }
370
+ /**
371
+ * Recompute `timeline.duration_ms` as the max end (abs + duration) across main,
372
+ * speech, and caption lanes (BGM does not extend the timeline).
373
+ *
374
+ * BGM has no authoritative duration (RFC 02 / `reference/16` §0b): its effective
375
+ * length is always the timeline total, so it is not written back here — the
376
+ * projection derives it from `timeline.duration_ms` on read (`partDurationMs`
377
+ * returns the timeline total for a bgm part). The empty-document 20s skeleton is
378
+ * applied at that read step, not stored.
379
+ */
380
+ function recalculateTimelineDuration(doc) {
381
+ const max = Math.max(laneEndMs(doc, doc.main_track), laneEndMs(doc, doc.speech_track), laneEndMs(doc, doc.caption_track));
382
+ doc.timeline.duration_ms = max;
383
+ }
384
+ /**
385
+ * Resolve one speech overlap by shifting the overlapping speech's host video
386
+ * (and every clip after it) right. Returns true when one overlap was resolved;
387
+ * callers loop until it returns false. The compared range is the speech merged
388
+ * with its captions: start = min(speech.start, captions.start) (FE behavior),
389
+ * end = max(speech.end, captions.end).
390
+ */
391
+ function resolveSpeechOverlapByShiftingVideos(doc) {
392
+ const speeches = doc.speech_track;
393
+ for (let i = 1; i < speeches.length; i++) {
394
+ const prev = speechWithCaptionsRange(doc, speeches[i - 1]);
395
+ const curr = speechWithCaptionsRange(doc, speeches[i]);
396
+ if (prev == null || curr == null) continue;
397
+ if (curr.start >= prev.end) continue;
398
+ const overlapMs = Math.round(prev.end - curr.start);
399
+ const hostId = hostVideoOf(doc, speeches[i].part_id);
400
+ if (hostId == null) continue;
401
+ const fromIndex = doc.main_track.findIndex((item) => item.part_id === hostId);
402
+ if (fromIndex < 0) continue;
403
+ for (let idx = fromIndex; idx < doc.main_track.length; idx++) doc.main_track[idx].abs_time_position += overlapMs;
404
+ syncAggregatedClipsTimePosition(doc);
405
+ return true;
406
+ }
407
+ return false;
408
+ }
409
+ /**
410
+ * Resolve speech overlaps for one cascade pass. Mirrors the authoritative FE
411
+ * `ensureNoOverlappingClips`, which is documented to resolve AT MOST ONE overlap
412
+ * per cascade and is invoked exactly once at every FE call site — the supported
413
+ * ops each produce at most one new overlap. It is NOT a fixpoint loop: shifting a
414
+ * host right also moves every speech anchored to it, so two speeches sharing a
415
+ * host can never be separated by shifting. Looping to a "fixed point" there does
416
+ * not converge — it accumulates the same overlap every iteration and pushes the
417
+ * clip arbitrarily far right (e.g. a sped-up clip landing at ~287k ms instead of
418
+ * its seamless slot). A single pass matches FE product behavior and terminates.
419
+ */
420
+ function resolveAllSpeechOverlaps(doc) {
421
+ resolveSpeechOverlapByShiftingVideos(doc);
422
+ }
423
+ /**
424
+ * Make the main track gapless by adjusting/merging empty placeholder clips or
425
+ * inserting new ones between real clips. Mirrors the harness four-case rule, but
426
+ * the merge of two adjacent empty clips keeps the earlier clip (harness Case 4).
427
+ * Requires `makeEmptyPart` to mint a placeholder part (the editor supplies an
428
+ * id generator).
429
+ */
430
+ function fillMainTrackTimeGaps(doc, makeEmptyPart) {
431
+ const items = doc.main_track;
432
+ let i = 0;
433
+ while (i < items.length) {
434
+ const current = items[i];
435
+ const currentPart = doc.part_library[current.part_id];
436
+ if (currentPart == null) {
437
+ i += 1;
438
+ continue;
439
+ }
440
+ const currentEnd = current.abs_time_position + partDurationMs(doc, current.part_id);
441
+ if (i + 1 >= items.length) break;
442
+ const next = items[i + 1];
443
+ const nextPart = doc.part_library[next.part_id];
444
+ if (nextPart == null) {
445
+ i += 1;
446
+ continue;
447
+ }
448
+ const gap = next.abs_time_position - currentEnd;
449
+ if (gap > 0) {
450
+ if (isEmptyVideoClip(currentPart)) {
451
+ extendEmptyClip(currentPart.video_clip, gap);
452
+ continue;
453
+ }
454
+ if (isEmptyVideoClip(nextPart)) {
455
+ next.abs_time_position -= gap;
456
+ extendEmptyClip(nextPart.video_clip, gap);
457
+ i += 1;
458
+ continue;
459
+ }
460
+ const { partId } = makeEmptyPart(gap);
461
+ doc.part_library[partId] = { video_clip: {
462
+ id: partId,
463
+ kind: "video_clip",
464
+ play_in: 0,
465
+ play_out: gap,
466
+ volume: 0,
467
+ origin_media_id: ""
468
+ } };
469
+ items.splice(i + 1, 0, {
470
+ part_id: partId,
471
+ time_position: { mode: "sequential" },
472
+ abs_time_position: currentEnd
473
+ });
474
+ i += 1;
475
+ continue;
476
+ }
477
+ if (gap === 0 && isEmptyVideoClip(currentPart) && isEmptyVideoClip(nextPart)) {
478
+ extendEmptyClip(currentPart.video_clip, partDurationMs(doc, next.part_id));
479
+ items.splice(i + 1, 1);
480
+ delete doc.part_library[next.part_id];
481
+ continue;
482
+ }
483
+ i += 1;
484
+ }
485
+ }
486
+ function indexByPart(items) {
487
+ const map = /* @__PURE__ */ new Map();
488
+ for (const item of items) map.set(item.part_id, item);
489
+ return map;
490
+ }
491
+ function laneEndMs(doc, items) {
492
+ let max = 0;
493
+ for (const item of items) {
494
+ if (doc.part_library[item.part_id] == null) continue;
495
+ const end = item.abs_time_position + partDurationMs(doc, item.part_id);
496
+ if (end > max) max = end;
497
+ }
498
+ return max;
499
+ }
500
+ function extendEmptyClip(clip, byMs) {
501
+ clip.play_out = safeDurationMs(clip.play_out) + byMs;
502
+ }
503
+ /** speech merged with its captions: start = min, end = max. */
504
+ function speechWithCaptionsRange(doc, speechItem) {
505
+ const speech = doc.part_library[speechItem.part_id]?.speech;
506
+ if (speech == null) return null;
507
+ let start = speechItem.abs_time_position;
508
+ let end = speechItem.abs_time_position + safeDurationMs(speech.media_duration_ms);
509
+ for (const captionId of captionIdsOf(speech)) {
510
+ const captionItem = doc.caption_track.find((item) => item.part_id === captionId);
511
+ const captionPart = doc.part_library[captionId]?.caption;
512
+ if (captionItem == null || captionPart == null) continue;
513
+ const cStart = captionItem.abs_time_position;
514
+ const cEnd = captionItem.abs_time_position + safeDurationMs(captionPart.initial_duration_ms);
515
+ if (cStart < start) start = cStart;
516
+ if (cEnd > end) end = cEnd;
517
+ }
518
+ return {
519
+ start,
520
+ end
521
+ };
522
+ }
523
+ function captionIdsOf(speech) {
524
+ return (speech.caption_ids ?? []).filter((id) => id != null);
525
+ }
526
+ function hostVideoOf(doc, speechPartId) {
527
+ for (const aggregation of doc.aggregations) if (aggregation.attachments.some((att) => att.part_id === speechPartId)) return aggregation.body_part_id;
528
+ return null;
529
+ }
530
+ //#endregion
531
+ //#region src/timeline-core/entrypoints.ts
532
+ /**
533
+ * The full solve pipeline: arrange → sync → reassign → resolve-overlap →
534
+ * fill-gaps → recalc. The single cascade the read-side projection runs; ops
535
+ * never call it (they write only facts — RFC 02 §7/§10).
536
+ */
537
+ function cascadeAfterVideoClipChanges(doc, makeEmptyPart) {
538
+ arrangeMainTrackSeamlessly(doc);
539
+ syncAggregatedClipsTimePosition(doc);
540
+ reassignSpeechesToVideoClipsByTime(doc);
541
+ resolveAllSpeechOverlaps(doc);
542
+ fillMainTrackTimeGaps(doc, makeEmptyPart);
543
+ recalculateTimelineDuration(doc);
544
+ }
545
+ //#endregion
546
+ //#region src/timeline-core/bridge.ts
547
+ /**
548
+ * Solve a `VideoDocument` (authoritative, position-only) into its derived
549
+ * read-view: absolute time per item, `part_aggregations`, and total duration.
550
+ * This is the read side of the single-directional flow — never written back.
551
+ */
552
+ function solveVideoDocument(document) {
553
+ const doc = videoDocumentToTimelineDoc(document);
554
+ let counter = 0;
555
+ const derivedFillerPartIds = /* @__PURE__ */ new Set();
556
+ if (document.meta.schema_version === "video-document/entity-projection-v1") {
557
+ for (const track of document.tracks ?? []) if (track.items?.some((item) => item.time_position.mode === "sequential" || item.time_position.mode === "anchored" && !Number.isSafeInteger(item.fallback_abs_ms))) throw new Error("An entity timeline projection must contain resolved placement facts");
558
+ recalculateTimelineDuration(doc);
559
+ } else cascadeAfterVideoClipChanges(doc, () => {
560
+ const partId = `empty_${counter++}`;
561
+ derivedFillerPartIds.add(partId);
562
+ return { partId };
563
+ });
564
+ const absByPartId = /* @__PURE__ */ new Map();
565
+ for (const item of doc.main_track) absByPartId.set(item.part_id, item.abs_time_position);
566
+ for (const item of doc.speech_track) absByPartId.set(item.part_id, item.abs_time_position);
567
+ for (const item of doc.caption_track) absByPartId.set(item.part_id, item.abs_time_position);
568
+ for (const item of doc.bgm_track) absByPartId.set(item.part_id, item.abs_time_position);
569
+ return {
570
+ absByPartId,
571
+ aggregations: doc.aggregations.map((aggregation) => ({
572
+ body_part_id: aggregation.body_part_id,
573
+ attachments: aggregation.attachments.map((a) => ({
574
+ part_id: a.part_id,
575
+ relative_time_position: a.relative_time_position
576
+ }))
577
+ })),
578
+ durationMs: doc.timeline.duration_ms,
579
+ partLibrary: doc.part_library,
580
+ derivedFillerPartIds
581
+ };
582
+ }
583
+ function videoDocumentToTimelineDoc(document) {
584
+ const partLibrary = {};
585
+ for (const [partId, part] of Object.entries(document.part_library ?? {})) partLibrary[partId] = part;
586
+ const laneItems = (track) => (track?.items ?? []).map((item) => ({
587
+ part_id: item.part_id,
588
+ time_position: item.time_position,
589
+ abs_time_position: seedAbs(item.time_position, item.fallback_abs_ms),
590
+ fallback_abs_ms: item.fallback_abs_ms
591
+ }));
592
+ const tracks = document.tracks ?? [];
593
+ let mainItems = [];
594
+ let speechItems = [];
595
+ let bgmItems = [];
596
+ let captionItems = [];
597
+ for (const t of tracks) switch (t.parts_kind) {
598
+ case "video_clip":
599
+ mainItems = mainItems.concat(laneItems(t));
600
+ break;
601
+ case "speech":
602
+ speechItems = speechItems.concat(laneItems(t));
603
+ break;
604
+ case "bgm":
605
+ bgmItems = bgmItems.concat(laneItems(t));
606
+ break;
607
+ case "caption":
608
+ captionItems = captionItems.concat(laneItems(t));
609
+ break;
610
+ }
611
+ return {
612
+ main_track: mainItems,
613
+ speech_track: speechItems,
614
+ caption_track: captionItems,
615
+ bgm_track: bgmItems,
616
+ part_library: partLibrary,
617
+ aggregations: seedAggregations(mainItems, speechItems),
618
+ timeline: {
619
+ duration_ms: 0,
620
+ unit_time_ms: document.timeline?.unit_time_ms ?? 0
621
+ }
622
+ };
623
+ }
624
+ /**
625
+ * Seed an item's solve-variable `abs_time_position` from its `time_position` (and
626
+ * the `fallback_abs_ms` snapshot carried beside it): `absolute` → `offsetMs`;
627
+ * `anchored` → `fallbackAbsMs` (last solved position, also the orphan-recovery
628
+ * anchor when the host is gone); `sequential` → 0 (the cascade lays the main
629
+ * track out head-to-tail). The cascade then resolves anchored items against their
630
+ * host, so this seed only needs to put each item somewhere plausible for the
631
+ * first reassign/overlap pass.
632
+ */
633
+ function seedAbs(position, fallbackAbsMs) {
634
+ switch (position.mode) {
635
+ case "absolute": return Math.round(position.offsetMs);
636
+ case "anchored": return Math.round(fallbackAbsMs ?? 0);
637
+ case "sequential": return 0;
638
+ }
639
+ }
640
+ /**
641
+ * Build the cascade `aggregations` parent-map from anchored items: an anchored
642
+ * item's `anchorPartId` is the host's `part_id` (= `body_part_id`), `offsetMs`
643
+ * is `relative_time_position`. Only main-track (video) hosts form aggregations;
644
+ * caption→speech relations are recomputed by the cascade from `caption.start_ms`.
645
+ */
646
+ function seedAggregations(mainItems, speechItems) {
647
+ const mainPartIds = new Set(mainItems.map((item) => item.part_id));
648
+ const byHost = /* @__PURE__ */ new Map();
649
+ const order = [];
650
+ for (const speech of speechItems) {
651
+ if (speech.time_position.mode !== "anchored") continue;
652
+ const host = speech.time_position.anchorPartId;
653
+ if (!mainPartIds.has(host)) continue;
654
+ let aggregation = byHost.get(host);
655
+ if (aggregation == null) {
656
+ aggregation = {
657
+ body_part_id: host,
658
+ attachments: []
659
+ };
660
+ byHost.set(host, aggregation);
661
+ order.push(host);
662
+ }
663
+ aggregation.attachments.push({
664
+ part_id: speech.part_id,
665
+ relative_time_position: speech.time_position.offsetMs
666
+ });
667
+ }
668
+ return order.map((host) => byHost.get(host));
669
+ }
670
+ /**
671
+ * The four lanes in top-to-bottom stack order — the order a `tracks` list holds
672
+ * them in (see {@link laneRank}).
673
+ *
674
+ * Exported so a document can be seeded with all four lanes up front. That seed is
675
+ * not cosmetic: {@link ensureLaneTrack} is find-then-mint over a `LoroMovableList`,
676
+ * so two concurrent writers that each mint the same absent lane both keep their
677
+ * row, and the merged document holds two tracks for one lane. For the main lane
678
+ * that is fatal — `videoDocumentSchema` allows at most one `video_clip` track, so
679
+ * the merged document stops being projectable at all, symmetrically on both
680
+ * replicas. Pre-seeding every lane makes `ensureLaneTrack` always take its find
681
+ * branch, which removes the race by construction rather than by detection.
682
+ *
683
+ * What closes the race is that a track with the lane's `parts_kind` EXISTS — the
684
+ * lookup is by kind, not by id. So a seed is only safe if it covers every lane:
685
+ * a partial seed leaves the uncovered lanes exactly as exposed as before.
686
+ */
687
+ const LANE_KINDS_IN_STACK_ORDER = [
688
+ "caption",
689
+ "video_clip",
690
+ "speech",
691
+ "bgm"
692
+ ];
693
+ /**
694
+ * The conventional track id for a lane (`main_track`, `<kind>_track`). Shared by
695
+ * the up-front seed and {@link ensureLaneTrack}'s lazy mint so a document's lane
696
+ * ids do not depend on which of the two created the track.
697
+ *
698
+ * Ids are cosmetic to the merge itself — lane lookup goes by `parts_kind`, so
699
+ * drifting them apart would not reopen the concurrent-mint race. They matter to
700
+ * readers that address a lane by id (the FE editor's panes, fixtures), which is
701
+ * why there is one convention rather than two.
702
+ */
703
+ function laneTrackId(kind) {
704
+ return kind === "video_clip" ? "main_track" : `${kind}_track`;
705
+ }
706
+ /**
707
+ * Lane-stacking rank for the single `tracks` list (reference/17 §4): caption
708
+ * (above) sits before the video_clip main track, which sits before speech / bgm
709
+ * (below). The `tracks` array is kept in this top-to-bottom order so a freshly
710
+ * minted track lands in the right place and the projection's three-pane rebuild
711
+ * stays deterministic.
712
+ */
713
+ function laneRank(kind) {
714
+ switch (kind) {
715
+ case "caption": return 0;
716
+ case "video_clip": return 1;
717
+ case "speech":
718
+ case "bgm": return 2;
719
+ default: return 3;
720
+ }
721
+ }
722
+ /**
723
+ * Insert a freshly-minted track into `tracks` at the position that keeps the
724
+ * lane-stacking order (caption → main → speech/bgm). Inserts before the first
725
+ * track whose rank is strictly greater, so same-rank tracks keep insertion order.
726
+ */
727
+ function insertTrackByLaneOrder(tracks, track) {
728
+ const rank = laneRank(track.parts_kind);
729
+ const at = tracks.findIndex((t) => laneRank(t?.parts_kind) > rank);
730
+ if (at < 0) tracks.push(track);
731
+ else tracks.splice(at, 0, track);
732
+ }
733
+ /**
734
+ * Locate a lane's track row in the single `tracks` list by kind, minting an empty
735
+ * row in lane-stacking order if absent (reference/17 §4: lane = `parts_kind`).
736
+ * Ops use this to write authoritative items onto the right lane. The track id
737
+ * comes from {@link laneTrackId}, shared with the up-front seed.
738
+ *
739
+ * The mint branch is a concurrency hazard, not a convenience: see
740
+ * {@link LANE_KINDS_IN_STACK_ORDER}. A document seeded with all four lanes never
741
+ * reaches it.
742
+ */
743
+ function ensureLaneTrack(draft, kind) {
744
+ draft.tracks ??= [];
745
+ let track = draft.tracks.find((t) => t?.parts_kind === kind);
746
+ if (track == null) {
747
+ track = {
748
+ id: laneTrackId(kind),
749
+ parts_kind: kind,
750
+ is_hidden: void 0,
751
+ items: []
752
+ };
753
+ insertTrackByLaneOrder(draft.tracks, track);
754
+ }
755
+ track.items ??= [];
756
+ return track;
757
+ }
758
+ /** Find a secondary lane's track row without minting it. */
759
+ function findLaneTrack(draft, kind) {
760
+ return (draft.tracks ?? []).find((t) => t?.parts_kind === kind);
761
+ }
762
+ //#endregion
763
+ //#region src/document/zod-schema.ts
764
+ const partKindSchema = z.enum([
765
+ "video_clip",
766
+ "speech",
767
+ "caption",
768
+ "bgm"
769
+ ]);
770
+ const finiteNumber = z.number().finite();
771
+ const trackItemTimePositionSchema = z.discriminatedUnion("mode", [
772
+ z.object({ mode: z.literal("sequential") }).passthrough(),
773
+ z.object({
774
+ mode: z.literal("anchored"),
775
+ anchorPartId: z.string(),
776
+ offsetMs: finiteNumber
777
+ }).passthrough(),
778
+ z.object({
779
+ mode: z.literal("absolute"),
780
+ offsetMs: finiteNumber
781
+ }).passthrough()
782
+ ]);
783
+ const trackItemSchema = z.object({
784
+ part_id: z.string(),
785
+ time_position: trackItemTimePositionSchema,
786
+ fallback_abs_ms: finiteNumber.optional()
787
+ }).passthrough();
788
+ const trackSchema = z.object({
789
+ id: z.string().optional(),
790
+ parts_kind: partKindSchema.optional(),
791
+ is_hidden: z.boolean().optional(),
792
+ items: z.array(trackItemSchema).optional()
793
+ }).passthrough();
794
+ const speedShiftSchema = z.object({
795
+ category: z.string().optional(),
796
+ mode: z.string().optional(),
797
+ config: z.object({ linear: z.object({ speed: finiteNumber.optional() }).passthrough().optional() }).passthrough().optional()
798
+ }).passthrough();
799
+ const videoClipPartSchema = z.object({
800
+ id: z.string().optional(),
801
+ kind: z.literal("video_clip"),
802
+ duration_ms: finiteNumber.optional(),
803
+ play_in: finiteNumber,
804
+ play_out: finiteNumber,
805
+ volume: finiteNumber,
806
+ origin_media_id: z.string(),
807
+ speed_shift: speedShiftSchema.optional()
808
+ }).passthrough();
809
+ const speechPartSchema = z.object({
810
+ id: z.string().optional(),
811
+ kind: z.literal("speech"),
812
+ media_duration_ms: finiteNumber,
813
+ duration_ms: finiteNumber.optional(),
814
+ audio_script: z.string(),
815
+ volume: finiteNumber,
816
+ audio_storage_key: z.string(),
817
+ origin_speech_id: z.string(),
818
+ voice: z.unknown(),
819
+ caption_ids: z.array(z.string())
820
+ }).passthrough();
821
+ const captionPartSchema = z.object({
822
+ id: z.string().optional(),
823
+ kind: z.literal("caption"),
824
+ initial_duration_ms: finiteNumber,
825
+ speech_part_id: z.string().optional(),
826
+ text: z.string(),
827
+ start_ms: finiteNumber,
828
+ style: z.object({
829
+ font_id: z.string().optional(),
830
+ font_size: finiteNumber.optional(),
831
+ font_color: z.string().optional(),
832
+ font_weight: finiteNumber.optional(),
833
+ entrance_animation: z.string().optional(),
834
+ entrance_animation_duration_ms: finiteNumber.optional(),
835
+ stroke_color: z.string().optional(),
836
+ stroke_width: finiteNumber.optional(),
837
+ position_x: finiteNumber.optional(),
838
+ position_y: finiteNumber.optional()
839
+ }).passthrough().optional()
840
+ }).passthrough();
841
+ const bgmPartSchema = z.object({
842
+ id: z.string().optional(),
843
+ kind: z.literal("bgm"),
844
+ audio_storage_key: z.string(),
845
+ volume: finiteNumber,
846
+ origin_media_id: z.string()
847
+ }).passthrough();
848
+ const partUnionSchema = z.union([
849
+ z.object({ video_clip: videoClipPartSchema }).passthrough(),
850
+ z.object({ speech: speechPartSchema }).passthrough(),
851
+ z.object({ caption: captionPartSchema }).passthrough(),
852
+ z.object({ bgm: bgmPartSchema }).passthrough()
853
+ ]).refine((part) => {
854
+ const p = part;
855
+ return [
856
+ "video_clip",
857
+ "speech",
858
+ "caption",
859
+ "bgm"
860
+ ].filter((k) => p[k] != null).length === 1;
861
+ }, { message: "A part_library value must have exactly one of video_clip / speech / caption / bgm" });
862
+ const videoDocumentMetaSchema = z.object({
863
+ schema_version: z.enum([VIDEO_DOCUMENT_SCHEMA_VERSION, ENTITY_TIMELINE_PROJECTION_SCHEMA_VERSION]),
864
+ draft_id: z.string().optional(),
865
+ project_id: z.string().optional(),
866
+ owner_id: z.string().optional(),
867
+ thumbnail_storage_key: z.string().optional(),
868
+ chat_session_id: z.string().optional(),
869
+ video_creation_settings: z.unknown().optional(),
870
+ version: finiteNumber.optional()
871
+ }).passthrough();
872
+ const videoDocumentSchema = z.object({
873
+ meta: videoDocumentMetaSchema,
874
+ timeline: z.object({ unit_time_ms: finiteNumber.optional() }).passthrough().optional(),
875
+ tracks: z.array(trackSchema).optional(),
876
+ part_library: z.record(z.string(), partUnionSchema).optional()
877
+ }).passthrough().refine((doc) => (doc.tracks ?? []).filter((t) => t.parts_kind === "video_clip").length <= 1, {
878
+ message: "A VideoDocument may have at most one video_clip (main) track",
879
+ path: ["tracks"]
880
+ });
881
+ //#endregion
882
+ //#region src/document/validation.ts
883
+ /**
884
+ * Business-level schema guard for `VideoDocument` (RFC 03 §9). It is the gate
885
+ * that decides whether an arbitrary value is a *legal* `VideoDocument` before it
886
+ * is written into Loro — distinct from two neighbours:
887
+ *
888
+ * - `zod-schema.ts` (`videoDocumentSchema`) checks structure/shape only; this
889
+ * file layers the business rules on top (part-kind match, reference integrity,
890
+ * `position.anchorPartId` targets, value ranges, identity uniqueness).
891
+ * - loro-mirror's own `validateSchema` (run on every `setState`) only checks the
892
+ * storage structure, never these business invariants.
893
+ *
894
+ * Projection (`projection.ts`) calls `assertValidVideoDocument` before solving a
895
+ * document into the legacy `VideoDraft`.
896
+ */
897
+ var VideoDocumentValidationError = class extends Error {
898
+ issues;
899
+ constructor(issues) {
900
+ super(`Invalid VideoDocument: ${issues.map((issue) => issue.message).join("; ")}`);
901
+ this.issues = issues;
902
+ this.name = "VideoDocumentValidationError";
903
+ }
904
+ };
905
+ /**
906
+ * Assert the document is legal enough to project. Only `error`-severity issues
907
+ * hard-reject; `recoverable` ones (dangling anchor / orphan, RFC 02 §11.1) are
908
+ * left for the projection to heal on read and do NOT throw. Use
909
+ * `validateVideoDocument` directly to inspect recoverable issues too.
910
+ */
911
+ function assertValidVideoDocument(document) {
912
+ const blocking = validateVideoDocument(document).filter((issue) => (issue.severity ?? "error") === "error");
913
+ if (blocking.length > 0) throw new VideoDocumentValidationError(blocking);
914
+ }
915
+ function validateVideoDocument(document) {
916
+ const parsed = videoDocumentSchema.safeParse(document);
917
+ if (!parsed.success) return parsed.error.issues.map((issue) => ({
918
+ code: "invalid_schema",
919
+ path: zodPath(issue.path),
920
+ message: issue.message
921
+ }));
922
+ const doc = parsed.data;
923
+ const issues = [];
924
+ const partLibrary = doc.part_library ?? {};
925
+ for (const [partId, part] of Object.entries(partLibrary)) {
926
+ if (!partUnionSchema.safeParse(part).success) {
927
+ issues.push({
928
+ code: "unknown_part_kind",
929
+ path: `/part_library/${partId}`,
930
+ message: `Part "${partId}" is not a supported part union`
931
+ });
932
+ continue;
933
+ }
934
+ const wrapperKind = getPartUnionKind(part);
935
+ const innerKind = getPartKind(part);
936
+ if (wrapperKind != null && innerKind != null && wrapperKind !== innerKind) issues.push({
937
+ code: "part_kind_mismatch",
938
+ path: `/part_library/${partId}`,
939
+ message: `Part "${partId}" wrapper kind "${wrapperKind}" does not match inner kind "${innerKind}"`
940
+ });
941
+ validatePartValues(partId, part, issues);
942
+ }
943
+ for (const [idx, track] of (doc.tracks ?? []).entries()) validateTrack(track, `tracks/${idx}`, partLibrary, issues, track.parts_kind === "video_clip");
944
+ validateLaneTrackUniqueness(doc, issues);
945
+ validateSpeechCaptionReferences(partLibrary, issues);
946
+ validatePositionReferences(doc, partLibrary, issues);
947
+ return issues;
948
+ }
949
+ /**
950
+ * Report a duplicated lane: two tracks sharing the same `id`, or two secondary
951
+ * tracks of the same `parts_kind`.
952
+ *
953
+ * Found by the Phase 6 M0/S2 concurrency probe. `ensureLaneTrack` locates a lane
954
+ * by `parts_kind` and mints `<kind>_track` when absent, so two replicas that both
955
+ * start without (say) a caption track each create one; `tracks` is a
956
+ * `LoroMovableList`, so the merge keeps BOTH. The lane is then split across two
957
+ * same-id tracks, and the projection's `find`-by-kind returns only one of them —
958
+ * so an item can be authoritative yet invisible in the pane a reader looks at.
959
+ *
960
+ * Flagged **recoverable**, not `error`, for two reasons. It is a reachable
961
+ * outcome of ordinary concurrent editing, and hard-rejecting would make an
962
+ * unavoidable merge result un-projectable — the same argument §11.1 makes for
963
+ * orphans. And the projection already tolerates it: `solveVideoDocument`
964
+ * concatenates every track of a kind, so the cascade sees all items regardless.
965
+ * The defect is a *reader-visible* split, so the right response is to surface it
966
+ * (loudly, in a channel someone reads), not to block the document.
967
+ *
968
+ * The main track is exempt from the kind check: `parts_kind === 'video_clip'` may
969
+ * legitimately appear on several tracks (the projection treats only the first as
970
+ * the main pane), so only its `id` collision is reported.
971
+ */
972
+ function validateLaneTrackUniqueness(doc, issues) {
973
+ const tracks = doc.tracks ?? [];
974
+ const seenIds = /* @__PURE__ */ new Set();
975
+ const seenSecondaryKinds = /* @__PURE__ */ new Set();
976
+ for (const [idx, track] of tracks.entries()) {
977
+ const id = track?.id;
978
+ if (id != null && id !== "") if (seenIds.has(id)) issues.push({
979
+ code: "duplicate_lane_track",
980
+ path: `/tracks/${idx}/id`,
981
+ message: `Track "tracks/${idx}" reuses track id "${id}"`,
982
+ severity: "recoverable"
983
+ });
984
+ else seenIds.add(id);
985
+ const kind = track?.parts_kind;
986
+ if (kind == null || kind === "video_clip") continue;
987
+ if (seenSecondaryKinds.has(kind)) issues.push({
988
+ code: "duplicate_lane_track",
989
+ path: `/tracks/${idx}/parts_kind`,
990
+ message: `Lane "${kind}" is split across more than one track (at "tracks/${idx}")`,
991
+ severity: "recoverable"
992
+ });
993
+ else seenSecondaryKinds.add(kind);
994
+ }
995
+ }
996
+ /**
997
+ * An `anchored` item whose `anchorPartId` no longer exists in the library is the
998
+ * orphan condition (RFC 02 §4/§11.1) — e.g. a speech whose host video, or a
999
+ * caption whose host speech, was concurrently deleted while this item was being
1000
+ * reparented in. This is flagged as a **recoverable** issue, NOT a hard error:
1001
+ * the projection heals it on read (the item's `fallback_abs_ms` snapshot seeds
1002
+ * its absolute position, then the cascade reassigns it to the nearest available
1003
+ * host, or it stays put as an absolute item). Reporting it here keeps the orphan
1004
+ * observable without blocking projection — the opposite of a hard reject, which
1005
+ * would make an inevitable concurrent-delete outcome un-projectable (§11.1).
1006
+ */
1007
+ function validatePositionReferences(doc, partLibrary, issues) {
1008
+ const tracks = (doc.tracks ?? []).map((track, idx) => ({
1009
+ track,
1010
+ path: `tracks/${idx}`
1011
+ }));
1012
+ for (const { track, path } of tracks) for (const [idx, item] of (track?.items ?? []).entries()) {
1013
+ if (item.time_position.mode !== "anchored") continue;
1014
+ if (partLibrary[item.time_position.anchorPartId] == null) issues.push({
1015
+ code: "invalid_position_anchor",
1016
+ path: `/${path}/items/${idx}/time_position/anchorPartId`,
1017
+ message: `Track item "${path}/${idx}" anchors to missing part "${item.time_position.anchorPartId}"`,
1018
+ severity: "recoverable"
1019
+ });
1020
+ }
1021
+ }
1022
+ function validatePartValues(partId, part, issues) {
1023
+ const payload = part.video_clip ?? part.speech ?? part.caption ?? part.bgm ?? void 0;
1024
+ if (payload == null) return;
1025
+ if ("duration_ms" in payload && payload.duration_ms != null && payload.duration_ms < 0) issues.push({
1026
+ code: "invalid_part_value",
1027
+ path: `/part_library/${partId}/duration_ms`,
1028
+ message: `Part "${partId}" has negative duration_ms`
1029
+ });
1030
+ if ("volume" in payload && payload.volume != null && !Number.isFinite(payload.volume)) issues.push({
1031
+ code: "invalid_part_value",
1032
+ path: `/part_library/${partId}/volume`,
1033
+ message: `Part "${partId}" has non-finite volume`
1034
+ });
1035
+ if (part.video_clip != null) {
1036
+ const { play_in, play_out } = part.video_clip;
1037
+ if (play_in != null && play_in < 0) issues.push({
1038
+ code: "invalid_part_value",
1039
+ path: `/part_library/${partId}/play_in`,
1040
+ message: `Video clip "${partId}" has negative play_in`
1041
+ });
1042
+ if (play_out != null && play_in != null && play_out < play_in) issues.push({
1043
+ code: "invalid_part_value",
1044
+ path: `/part_library/${partId}/play_out`,
1045
+ message: `Video clip "${partId}" has play_out before play_in`
1046
+ });
1047
+ }
1048
+ }
1049
+ function validateTrack(track, path, partLibrary, issues, isMainTrack) {
1050
+ if (track == null) return;
1051
+ const seenPartIds = /* @__PURE__ */ new Set();
1052
+ for (const [idx, item] of (track.items ?? []).entries()) {
1053
+ const partId = item.part_id;
1054
+ if (partId != null && partId !== "") if (seenPartIds.has(partId)) issues.push({
1055
+ code: "duplicate_track_item_identity",
1056
+ path: `/${path}/items/${idx}/part_id`,
1057
+ message: `Track item "${path}/${idx}" reuses part_id "${partId}"`
1058
+ });
1059
+ else seenPartIds.add(partId);
1060
+ if (partId == null || partId === "") {
1061
+ issues.push({
1062
+ code: "missing_part_reference",
1063
+ path: `/${path}/items/${idx}/part_id`,
1064
+ message: `Track item "${path}/${idx}" has no part_id`
1065
+ });
1066
+ continue;
1067
+ }
1068
+ const part = partLibrary[partId];
1069
+ if (part == null) {
1070
+ issues.push({
1071
+ code: "missing_part_reference",
1072
+ path: `/${path}/items/${idx}/part_id`,
1073
+ message: `Track item "${path}/${idx}" references missing part "${partId}"`
1074
+ });
1075
+ continue;
1076
+ }
1077
+ const partKind = getPartKind(part);
1078
+ if (isMainTrack && partKind !== "video_clip") issues.push({
1079
+ code: "main_track_non_video_clip",
1080
+ path: `/${path}/items/${idx}/part_id`,
1081
+ message: `Main track item "${path}/${idx}" references non-video part "${partId}"`
1082
+ });
1083
+ if (track.parts_kind != null && partKind != null && track.parts_kind !== partKind) issues.push({
1084
+ code: "track_kind_mismatch",
1085
+ path: `/${path}/items/${idx}/part_id`,
1086
+ message: `Track "${path}" expects "${track.parts_kind}" but part "${partId}" is "${partKind}"`
1087
+ });
1088
+ }
1089
+ }
1090
+ /**
1091
+ * Check speech↔caption references. A reference to a *missing* part (speech's
1092
+ * caption_ids → deleted caption, or caption's speech_part_id → deleted speech)
1093
+ * is the orphan condition (RFC 02 §11.1): reported as **recoverable** so the
1094
+ * projection can heal it on read, not block. A *back-pointer mismatch* (caption
1095
+ * exists but does not point back) is data corruption, kept as a hard error.
1096
+ */
1097
+ function validateSpeechCaptionReferences(partLibrary, issues) {
1098
+ for (const [partId, part] of Object.entries(partLibrary)) {
1099
+ if (part.speech != null) for (const captionId of part.speech.caption_ids ?? []) {
1100
+ const caption = partLibrary[captionId]?.caption;
1101
+ if (caption == null) {
1102
+ issues.push({
1103
+ code: "invalid_speech_caption_reference",
1104
+ path: `/part_library/${partId}/speech/caption_ids`,
1105
+ message: `Speech "${partId}" references missing caption "${captionId}"`,
1106
+ severity: "recoverable"
1107
+ });
1108
+ continue;
1109
+ }
1110
+ if (caption.speech_part_id !== partId) issues.push({
1111
+ code: "invalid_speech_caption_reference",
1112
+ path: `/part_library/${captionId}/caption/speech_part_id`,
1113
+ message: `Caption "${captionId}" does not point back to speech "${partId}"`
1114
+ });
1115
+ }
1116
+ if (part.caption != null) {
1117
+ const speechId = part.caption.speech_part_id;
1118
+ if (speechId != null && partLibrary[speechId]?.speech == null) issues.push({
1119
+ code: "invalid_speech_caption_reference",
1120
+ path: `/part_library/${partId}/caption/speech_part_id`,
1121
+ message: `Caption "${partId}" references missing speech "${speechId}"`,
1122
+ severity: "recoverable"
1123
+ });
1124
+ }
1125
+ }
1126
+ }
1127
+ function getPartUnionKind(part) {
1128
+ if (part.video_clip != null) return "video_clip";
1129
+ if (part.speech != null) return "speech";
1130
+ if (part.caption != null) return "caption";
1131
+ if (part.bgm != null) return "bgm";
1132
+ }
1133
+ function getPartKind(part) {
1134
+ return part.video_clip?.kind ?? part.speech?.kind ?? part.caption?.kind ?? part.bgm?.kind;
1135
+ }
1136
+ function zodPath(path) {
1137
+ if (path.length === 0) return "/";
1138
+ return `/${path.map(String).join("/")}`;
1139
+ }
1140
+ //#endregion
1141
+ //#region src/document/projection.ts
1142
+ /**
1143
+ * Projection between the authoritative `VideoDocument` and the legacy
1144
+ * `VideoDraft` read-view (RFC 02 §5/§7). Both directions live here:
1145
+ *
1146
+ * - `toVideoDocument` ingests a `VideoDraft`, deriving each item's `position`
1147
+ * from the legacy absolute layout + aggregations; derived values (abs time,
1148
+ * `part_aggregations`, total duration) are dropped.
1149
+ * - `fromVideoDocument` solves a `VideoDocument` back into a `VideoDraft` via the
1150
+ * timeline-core cascade, re-deriving exactly those values.
1151
+ *
1152
+ * Business validation lives in `validation.ts`; `fromVideoDocument` asserts a
1153
+ * valid document before solving.
1154
+ */
1155
+ /**
1156
+ * Ingest the legacy `VideoDraft` into the authoritative `VideoDocument`,
1157
+ * deriving each item's `time_position` from the legacy absolute layout +
1158
+ * aggregations (RFC 02 §4/§5). Absolute time, `part_aggregations`, and total
1159
+ * duration are dropped — they are re-derived by the projection.
1160
+ */
1161
+ function toVideoDocument(draft) {
1162
+ const speechHost = buildSpeechHostMap(draft.part_aggregations);
1163
+ const partLibrary = draft.part_library ?? {};
1164
+ const toTrack = (track, isMain) => {
1165
+ if (track == null) return void 0;
1166
+ return {
1167
+ id: track.id,
1168
+ parts_kind: track.parts_kind,
1169
+ is_hidden: track.is_hidden,
1170
+ items: (track.items ?? []).map((item) => deriveItem(item, isMain, speechHost, partLibrary))
1171
+ };
1172
+ };
1173
+ const mainTrack = toTrack(draft.main_track, true);
1174
+ const tracks = [
1175
+ ...(draft.above_main_tracks ?? []).map((t) => toTrack(t, false)),
1176
+ ...mainTrack == null ? [] : [mainTrack],
1177
+ ...(draft.below_main_tracks ?? []).map((t) => toTrack(t, false))
1178
+ ];
1179
+ return {
1180
+ meta: {
1181
+ schema_version: VIDEO_DOCUMENT_SCHEMA_VERSION,
1182
+ draft_id: draft.id,
1183
+ project_id: draft.project_id,
1184
+ owner_id: draft.owner_id,
1185
+ thumbnail_storage_key: draft.thumbnail_storage_key,
1186
+ chat_session_id: draft.chat_session_id,
1187
+ video_creation_settings: clone$1(draft.video_creation_settings),
1188
+ version: draft.version
1189
+ },
1190
+ timeline: draft.timeline == null ? void 0 : { unit_time_ms: draft.timeline.unit_time_ms },
1191
+ tracks,
1192
+ part_library: toAuthoritativePartLibrary(draft.part_library)
1193
+ };
1194
+ }
1195
+ /**
1196
+ * Map the legacy `VideoDraft` part library into the authoritative shape
1197
+ * (reference/17 §5): the authoritative parts store no derived part-level
1198
+ * duration, so the read-view `duration_ms` is dropped from video / speech / bgm.
1199
+ * For a caption the legacy `duration_ms` is the generation-time length, stored
1200
+ * authoritatively as `initial_duration_ms`.
1201
+ */
1202
+ function toAuthoritativePartLibrary(partLibrary) {
1203
+ if (partLibrary == null) return void 0;
1204
+ const out = {};
1205
+ for (const [partId, part] of Object.entries(partLibrary)) {
1206
+ if (typeof part !== "object" || part == null) continue;
1207
+ if (part.video_clip != null) out[partId] = { video_clip: withTrimWindowFromDuration(part.video_clip) };
1208
+ else if (part.speech != null) {
1209
+ const { duration_ms, rest } = splitDurationMs(part.speech);
1210
+ out[partId] = { speech: {
1211
+ ...rest,
1212
+ media_duration_ms: rest.media_duration_ms ?? duration_ms
1213
+ } };
1214
+ } else if (part.caption != null) {
1215
+ const { duration_ms, rest } = splitDurationMs(part.caption);
1216
+ out[partId] = { caption: {
1217
+ ...rest,
1218
+ initial_duration_ms: rest.initial_duration_ms ?? duration_ms
1219
+ } };
1220
+ } else if (part.bgm != null) out[partId] = { bgm: omitDurationMs(part.bgm) };
1221
+ }
1222
+ return out;
1223
+ }
1224
+ /**
1225
+ * Build a speech-host map from a part_aggregations list. Used by
1226
+ * `toVideoDocument` (legacy VideoDraft → VideoDocument) to recover each speech's
1227
+ * host video clip and relative offset. Aggregation items with null ids are
1228
+ * skipped (malformed input tolerance).
1229
+ */
1230
+ function buildSpeechHostMap(aggregations) {
1231
+ const map = /* @__PURE__ */ new Map();
1232
+ for (const aggregation of aggregations ?? []) {
1233
+ const host = aggregation.body_part_id;
1234
+ if (host == null) continue;
1235
+ for (const attachment of aggregation.attachments ?? []) {
1236
+ if (attachment.part_id == null) continue;
1237
+ map.set(attachment.part_id, {
1238
+ hostPartId: host,
1239
+ offsetMs: Math.round(attachment.relative_time_position ?? 0)
1240
+ });
1241
+ }
1242
+ }
1243
+ return map;
1244
+ }
1245
+ /**
1246
+ * Derive `time_position` (and `fallbackAbsMs` for anchored items) from a legacy
1247
+ * absolute time + pre-built speech-host map + part library. The three-branch
1248
+ * rule (RFC 02 §4/§5, reference/17 §3):
1249
+ *
1250
+ * 1. main-track → `sequential`
1251
+ * 2. speech/attachment (part_id in speechHost) → `anchored(host, offsetMs)`
1252
+ * 3. caption (part has `speech_part_id`) → `anchored(speech, start_ms)`
1253
+ * 4. everything else → `absolute(abs)`
1254
+ *
1255
+ * `partLibrary` values may be `PartUnion | string | undefined` (draft raw
1256
+ * form); only object-typed entries are inspected for `caption`.
1257
+ */
1258
+ function derivePositionFromAbs(partId, abs, isMain, speechHost, partLibrary) {
1259
+ if (isMain) return { timePosition: { mode: "sequential" } };
1260
+ const host = speechHost.get(partId);
1261
+ if (host != null) return {
1262
+ timePosition: {
1263
+ mode: "anchored",
1264
+ anchorPartId: host.hostPartId,
1265
+ offsetMs: host.offsetMs
1266
+ },
1267
+ fallbackAbsMs: abs
1268
+ };
1269
+ const part = partLibrary[partId];
1270
+ const captionPart = typeof part === "object" && part != null ? part.caption : void 0;
1271
+ const speechId = captionPart?.speech_part_id ?? void 0;
1272
+ if (speechId != null) return {
1273
+ timePosition: {
1274
+ mode: "anchored",
1275
+ anchorPartId: speechId,
1276
+ offsetMs: Math.round(captionPart?.start_ms ?? 0)
1277
+ },
1278
+ fallbackAbsMs: abs
1279
+ };
1280
+ return { timePosition: {
1281
+ mode: "absolute",
1282
+ offsetMs: abs
1283
+ } };
1284
+ }
1285
+ /** Derive one item's authoritative `time_position` from its legacy abs + aggregation. */
1286
+ function deriveItem(item, isMain, speechHost, partLibrary) {
1287
+ const partId = item.part_id ?? "";
1288
+ const { timePosition, fallbackAbsMs } = derivePositionFromAbs(partId, Math.round(item.abs_time_position ?? 0), isMain, speechHost, partLibrary);
1289
+ return fallbackAbsMs == null ? {
1290
+ part_id: partId,
1291
+ time_position: timePosition
1292
+ } : {
1293
+ part_id: partId,
1294
+ time_position: timePosition,
1295
+ fallback_abs_ms: fallbackAbsMs
1296
+ };
1297
+ }
1298
+ /**
1299
+ * Project the authoritative `VideoDocument` back into the legacy `VideoDraft`
1300
+ * read-view, solving each item's absolute position, the `part_aggregations`, and
1301
+ * the total duration via the timeline-core cascade.
1302
+ *
1303
+ * The read-view is a compatibility contract, and the IDL declares
1304
+ * `Timeline.unit_time_ms` and `Track.is_hidden` `@required`
1305
+ * (`video_draft_comp.smithy`) — so a projection that omitted them handed every
1306
+ * consumer a payload that violated the shape it claims to satisfy, leaving each
1307
+ * one to invent its own default. Two already had, and they disagreed: the agent
1308
+ * harness used `unit_time_ms: 0`, director used a frame at 30fps. Defaulting here
1309
+ * makes the contract true at the one place that produces it.
1310
+ *
1311
+ * These two are defaultable because the projection knows their values on its own:
1312
+ * absent `unit_time_ms` means "no display granularity was ever stated" and absent
1313
+ * `is_hidden` means "this track was never hidden". `version` is NOT defaultable
1314
+ * here — its only honest value is server state (`update_seq`) that a
1315
+ * `VideoDocument` cannot see, so it stays absent and the HTTP layer fills it.
1316
+ */
1317
+ function fromVideoDocument(document) {
1318
+ assertValidVideoDocument(document);
1319
+ const view = solveVideoDocument(document);
1320
+ const tracks = document.tracks ?? [];
1321
+ const mainTrack = tracks.find((t) => t.parts_kind === "video_clip");
1322
+ const aboveTracks = tracks.filter((t) => t.parts_kind === "caption");
1323
+ const belowTracks = tracks.filter((t) => t.parts_kind === "speech" || t.parts_kind === "bgm");
1324
+ return {
1325
+ id: document.meta.draft_id,
1326
+ project_id: document.meta.project_id,
1327
+ owner_id: document.meta.owner_id,
1328
+ thumbnail_storage_key: document.meta.thumbnail_storage_key,
1329
+ timeline: {
1330
+ duration_ms: view.durationMs,
1331
+ unit_time_ms: document.timeline?.unit_time_ms ?? 33.333
1332
+ },
1333
+ video_creation_settings: clone$1(document.meta.video_creation_settings),
1334
+ chat_session_id: document.meta.chat_session_id,
1335
+ main_track: draftTrack(mainTrack, view.absByPartId),
1336
+ above_main_tracks: aboveTracks.map((t) => draftTrack(t, view.absByPartId)),
1337
+ below_main_tracks: belowTracks.map((t) => draftTrack(t, view.absByPartId)),
1338
+ part_aggregations: view.aggregations,
1339
+ part_library: toReadViewPartLibrary(view.partLibrary, view.durationMs, view.derivedFillerPartIds),
1340
+ version: document.meta.version
1341
+ };
1342
+ }
1343
+ /**
1344
+ * Map the authoritative part library into the `VideoDraft` read-view shape,
1345
+ * re-injecting the derived effective `duration_ms` downstream expects
1346
+ * (reference/17 §5/§7) — the authoritative parts store no part-level duration:
1347
+ *
1348
+ * - video clip → `(play_out - play_in) / speed` (`effectiveVideoClipDurationMs`)
1349
+ * - speech → its intrinsic `media_duration_ms` (no trim/speed)
1350
+ * - caption → its generation-time `initial_duration_ms`
1351
+ * - bgm → the timeline total (`durationMs`)
1352
+ *
1353
+ * Parts are deep-cloned; the engine extensions (`media_duration_ms`,
1354
+ * `initial_duration_ms`) are kept alongside the injected `duration_ms`.
1355
+ *
1356
+ * `derivedFillerPartIds` are skipped: the solve minted them to lay the track out
1357
+ * and they are not part of the document (see `fromVideoDocument`). An empty clip a
1358
+ * writer placed deliberately is NOT in that set and is projected normally.
1359
+ */
1360
+ function toReadViewPartLibrary(partLibrary, durationMs, derivedFillerPartIds) {
1361
+ const out = {};
1362
+ for (const [partId, part] of Object.entries(partLibrary)) {
1363
+ if (derivedFillerPartIds.has(partId)) continue;
1364
+ if (part.video_clip != null) {
1365
+ const clip = clone$1(part.video_clip);
1366
+ out[partId] = { video_clip: {
1367
+ ...clip,
1368
+ duration_ms: effectiveVideoClipDurationMs(clip)
1369
+ } };
1370
+ } else if (part.speech != null) {
1371
+ const speech = clone$1(part.speech);
1372
+ out[partId] = { speech: {
1373
+ ...speech,
1374
+ duration_ms: speech.media_duration_ms
1375
+ } };
1376
+ } else if (part.caption != null) {
1377
+ const caption = clone$1(part.caption);
1378
+ out[partId] = { caption: {
1379
+ ...caption,
1380
+ duration_ms: caption.initial_duration_ms
1381
+ } };
1382
+ } else if (part.bgm != null) out[partId] = { bgm: {
1383
+ ...clone$1(part.bgm),
1384
+ duration_ms: durationMs
1385
+ } };
1386
+ }
1387
+ return out;
1388
+ }
1389
+ /** Deep-clone a part payload, dropping the derived read-view `duration_ms`. */
1390
+ function omitDurationMs(part) {
1391
+ const { duration_ms: _drop, ...rest } = clone$1(part);
1392
+ return rest;
1393
+ }
1394
+ /**
1395
+ * Migrate a legacy video clip into the authoritative trim-window-only shape: the
1396
+ * authoritative part stores no `duration_ms`, so it is dropped — but a legacy
1397
+ * clip with no explicit trim window (`play_in` / `play_out` absent) would then
1398
+ * compute an effective length of 0 and vanish from the layout. When that clip
1399
+ * carried a legacy `duration_ms`, backfill it as the trim window (`play_in` 0,
1400
+ * `play_out = duration_ms`, no speed) so the effective length is preserved, then
1401
+ * drop `duration_ms`. A clip that already has a trim window keeps it verbatim.
1402
+ */
1403
+ function withTrimWindowFromDuration(clip) {
1404
+ const stripped = omitDurationMs(clip);
1405
+ if (clip.play_in != null || clip.play_out != null) return stripped;
1406
+ const legacyDuration = clip.duration_ms;
1407
+ if (legacyDuration == null || !Number.isFinite(legacyDuration)) return stripped;
1408
+ return {
1409
+ ...stripped,
1410
+ play_in: 0,
1411
+ play_out: legacyDuration
1412
+ };
1413
+ }
1414
+ /** Deep-clone a part payload, returning its `duration_ms` separately from the rest. */
1415
+ function splitDurationMs(part) {
1416
+ const { duration_ms, ...rest } = clone$1(part);
1417
+ return {
1418
+ duration_ms,
1419
+ rest
1420
+ };
1421
+ }
1422
+ function draftTrack(track, absByPartId) {
1423
+ if (track == null) return void 0;
1424
+ return {
1425
+ id: track.id,
1426
+ parts_kind: track.parts_kind,
1427
+ is_hidden: track.is_hidden ?? false,
1428
+ items: (track.items ?? []).map((item) => ({
1429
+ part_id: item.part_id,
1430
+ abs_time_position: absByPartId.get(item.part_id) ?? 0
1431
+ }))
1432
+ };
1433
+ }
1434
+ function clone$1(value) {
1435
+ if (Array.isArray(value)) return value.map((item) => clone$1(item));
1436
+ if (value != null && typeof value === "object") {
1437
+ const result = {};
1438
+ for (const [key, child] of Object.entries(value)) result[key] = clone$1(child);
1439
+ return result;
1440
+ }
1441
+ return value;
1442
+ }
1443
+ //#endregion
1444
+ //#region src/document/initial-document.ts
1445
+ /**
1446
+ * Build the `VideoDocument` a newly created project starts from: the caller's
1447
+ * project facts, no parts, and one empty track per lane.
1448
+ *
1449
+ * The empty lane tracks are the reason this function exists rather than callers
1450
+ * assembling a document inline. Lane tracks are otherwise minted lazily by the
1451
+ * first op that needs one (`ensureLaneTrack`), which is find-then-mint over a
1452
+ * CRDT list — so if the Agent's first generation and a user edit race on an
1453
+ * empty document, each mints its own copy of the same lane and the merge keeps
1454
+ * both. Two video_clip tracks violate the at-most-one-main-track rule, and the
1455
+ * merged document becomes unprojectable for *every* reader (editor, render,
1456
+ * share) with no winning replica to fall back on. Seeding all four lanes up
1457
+ * front means `ensureLaneTrack` only ever finds, never mints, so the race cannot
1458
+ * happen on a document created here.
1459
+ *
1460
+ * All four, not just the main lane: the secondary lanes fail less loudly (a
1461
+ * duplicate pane rather than an unreadable document), but they fail by the same
1462
+ * mechanism, and covering only the fatal one would leave three live races.
1463
+ *
1464
+ * `timeline` is left absent: `unit_time_ms` is a display concern the editor
1465
+ * supplies, and total duration is derived on read (RFC 02 §6), so a new document
1466
+ * has no timeline fact to state.
1467
+ */
1468
+ function buildInitialVideoDocument(facts) {
1469
+ return {
1470
+ meta: {
1471
+ schema_version: VIDEO_DOCUMENT_SCHEMA_VERSION,
1472
+ draft_id: facts.draftId,
1473
+ project_id: facts.projectId,
1474
+ owner_id: facts.ownerId,
1475
+ thumbnail_storage_key: void 0,
1476
+ chat_session_id: facts.chatSessionId,
1477
+ video_creation_settings: facts.videoCreationSettings,
1478
+ version: void 0
1479
+ },
1480
+ timeline: void 0,
1481
+ tracks: emptyLaneTracks(),
1482
+ part_library: {}
1483
+ };
1484
+ }
1485
+ /** One empty track per lane, in lane-stacking order, with the conventional ids. */
1486
+ function emptyLaneTracks() {
1487
+ return LANE_KINDS_IN_STACK_ORDER.map((kind) => ({
1488
+ id: laneTrackId(kind),
1489
+ parts_kind: kind,
1490
+ is_hidden: void 0,
1491
+ items: []
1492
+ }));
1493
+ }
1494
+ //#endregion
1495
+ //#region ../medeo-dsl/src/entities.ts
1496
+ function hasSequence(entity) {
1497
+ if (isKnownNonSequenceKind(entity.entityKind) || isReservedEntityKind(entity.entityKind)) return false;
1498
+ return isSequenceFields(entity);
1499
+ }
1500
+ function isSequenceFields(value) {
1501
+ if (!isRecord$1(value)) return false;
1502
+ const extent = value.extent;
1503
+ if (!isRecord$1(extent) || !("start" in extent) || extent.start === void 0) return false;
1504
+ if (extent.kind === "bounded" && (!("end" in extent) || extent.end === void 0)) return false;
1505
+ if (extent.kind === "unbounded" && "end" in extent) return false;
1506
+ if (extent.kind !== "bounded" && extent.kind !== "unbounded") return false;
1507
+ if (value.sampling !== "native" && value.sampling !== "constant" && value.sampling !== "derived") return false;
1508
+ return "coordinateSpace" in value && value.coordinateSpace !== void 0;
1509
+ }
1510
+ function isKnownSequenceKind(kind) {
1511
+ return isMediaAssetVariantKind(kind) || kind === "caption" || kind === "axvideo";
1512
+ }
1513
+ function isMediaAssetVariantKind(kind) {
1514
+ return kind === "video" || kind === "image" || kind === "audio" || kind === "voice";
1515
+ }
1516
+ function isKnownEntityKind(kind) {
1517
+ return isKnownSequenceKind(kind) || isKnownNonSequenceKind(kind);
1518
+ }
1519
+ function isReservedEntityKind(kind) {
1520
+ const normalized = kind.toLowerCase().replaceAll("-", "").replaceAll("_", "");
1521
+ return normalized === "speech" || normalized === "videodocument";
1522
+ }
1523
+ function isKnownNonSequenceKind(kind) {
1524
+ return kind === "timeline" || kind === "track" || kind === "clip" || kind === "asset" || kind === "sequence-marker" || kind === "viewport" || kind === "audio-script" || kind === "phonetic-script";
1525
+ }
1526
+ function isRecord$1(value) {
1527
+ return typeof value === "object" && value != null && !Array.isArray(value);
1528
+ }
1529
+ //#endregion
1530
+ //#region ../medeo-dsl/src/ids.ts
1531
+ function createEntityId(value) {
1532
+ return createId(value, "EntityId");
1533
+ }
1534
+ function createRelationId(value) {
1535
+ return createId(value, "RelationId");
1536
+ }
1537
+ function createId(value, label) {
1538
+ if (value.length === 0 || value.trim() !== value) throw new Error(`${label} must be a non-empty trimmed string`);
1539
+ return value;
1540
+ }
1541
+ //#endregion
1542
+ //#region ../medeo-dsl/src/json-values.ts
1543
+ const nativeObjectConstructorSource = Function.prototype.toString.call(Object);
1544
+ function isJsonObject(value) {
1545
+ return typeof value === "object" && value !== null && !Array.isArray(value) && isJsonValue(value, /* @__PURE__ */ new Set());
1546
+ }
1547
+ function isJsonValue(value, ancestors) {
1548
+ if (value === null || typeof value === "string" || typeof value === "boolean") return true;
1549
+ if (typeof value === "number") return Number.isFinite(value);
1550
+ if (typeof value !== "object") return false;
1551
+ if (!Array.isArray(value) && !isPlainObject(value)) return false;
1552
+ if (ancestors.has(value)) return false;
1553
+ ancestors.add(value);
1554
+ const valid = Array.isArray(value) ? value.every((item) => isJsonValue(item, ancestors)) : Object.values(value).every((item) => isJsonValue(item, ancestors));
1555
+ ancestors.delete(value);
1556
+ return valid;
1557
+ }
1558
+ /** Recognize an ordinary object from any VM realm without admitting class instances. */
1559
+ function isPlainObject(value) {
1560
+ try {
1561
+ const prototype = Object.getPrototypeOf(value);
1562
+ if (prototype === null) return true;
1563
+ if (Object.getPrototypeOf(prototype) !== null) return false;
1564
+ const constructor = Object.getOwnPropertyDescriptor(prototype, "constructor")?.value;
1565
+ return typeof constructor === "function" && constructor.prototype === prototype && Function.prototype.toString.call(constructor) === nativeObjectConstructorSource;
1566
+ } catch {
1567
+ return false;
1568
+ }
1569
+ }
1570
+ //#endregion
1571
+ //#region ../medeo-dsl/src/composition.ts
1572
+ /** Assembly fails explicitly when declared bases cannot provide valid, unambiguous content. */
1573
+ var ScriptCompositionError = class extends Error {
1574
+ code;
1575
+ entityId;
1576
+ constructor(code, entityId, message) {
1577
+ super(message);
1578
+ this.code = code;
1579
+ this.entityId = entityId;
1580
+ this.name = "ScriptCompositionError";
1581
+ }
1582
+ };
1583
+ /** Direct base IDs are structural fields, never ordinary Relation endpoints. */
1584
+ function variantBaseEntityIds(entity) {
1585
+ if (!Object.hasOwn(entity.payload, "baseEntityIds")) return [];
1586
+ const ids = entity.payload.baseEntityIds;
1587
+ if (!Array.isArray(ids) || ids.length === 0 || !ids.every((id) => typeof id === "string" && id.trim() === id && id.length > 0) || new Set(ids).size !== ids.length) throw new ScriptCompositionError("invalid_bases", entity.entityId, `Entity "${entity.entityId}" requires non-empty, unique baseEntityIds`);
1588
+ return ids.map(createEntityId);
1589
+ }
1590
+ /** Validate all base providers before applying explicit own fields; ordering cannot resolve ambiguity. */
1591
+ function assembleEntityContent(rows, entityId) {
1592
+ const byId = /* @__PURE__ */ new Map();
1593
+ for (const row of rows.entities) {
1594
+ if (byId.has(row.entityId)) throw new ScriptCompositionError("composition_ambiguous", row.entityId, `duplicate entity id "${row.entityId}"`);
1595
+ byId.set(row.entityId, row);
1596
+ }
1597
+ const active = /* @__PURE__ */ new Set();
1598
+ const cache = /* @__PURE__ */ new Map();
1599
+ const visit = (id) => {
1600
+ const cached = cache.get(id);
1601
+ if (cached !== void 0) return cached;
1602
+ if (active.has(id)) throw new ScriptCompositionError("composition_cycle", id, `Cyclic baseEntityIds at "${id}"`);
1603
+ const row = byId.get(id);
1604
+ if (row === void 0) throw new ScriptCompositionError("composition_dangling", id, `Missing base entity "${id}" in this document`);
1605
+ if (!isJsonObject(row.payload) || Object.hasOwn(row.payload, "entityId") || Object.hasOwn(row.payload, "entityKind")) throw new ScriptCompositionError("invalid_bases", id, `Entity "${id}" requires JSON own fields without reserved identity fields`);
1606
+ active.add(id);
1607
+ const inherited = {};
1608
+ const providers = /* @__PURE__ */ new Map();
1609
+ for (const baseId of variantBaseEntityIds(row)) {
1610
+ const base = visit(baseId);
1611
+ for (const [field, value] of Object.entries(base.payload)) {
1612
+ if (field === "baseEntityIds") continue;
1613
+ const previous = providers.get(field);
1614
+ if (previous !== void 0) throw new ScriptCompositionError("field_conflict", id, `Entity "${id}" field "${field}" conflicts between bases "${previous}" and "${baseId}"; own fields cannot resolve base ambiguity`);
1615
+ providers.set(field, baseId);
1616
+ Object.defineProperty(inherited, field, {
1617
+ value,
1618
+ enumerable: true,
1619
+ configurable: true,
1620
+ writable: true
1621
+ });
1622
+ }
1623
+ }
1624
+ const assembled = {
1625
+ ...row,
1626
+ payload: JSON.parse(JSON.stringify({
1627
+ ...inherited,
1628
+ ...row.payload
1629
+ }))
1630
+ };
1631
+ active.delete(id);
1632
+ cache.set(id, assembled);
1633
+ return assembled;
1634
+ };
1635
+ return visit(entityId);
1636
+ }
1637
+ /** Resolve a field's declaring entity after validating the entire composition. */
1638
+ function resolveEntityFieldOwner(rows, entityId, field) {
1639
+ assembleEntityContent(rows, entityId);
1640
+ const byId = new Map(rows.entities.map((row) => [row.entityId, row]));
1641
+ const find = (id) => {
1642
+ const row = byId.get(id);
1643
+ if (Object.hasOwn(row.payload, field)) return id;
1644
+ if (field === "baseEntityIds") return void 0;
1645
+ for (const baseId of variantBaseEntityIds(row)) {
1646
+ const owner = find(baseId);
1647
+ if (owner !== void 0) return owner;
1648
+ }
1649
+ };
1650
+ return find(entityId) ?? entityId;
1651
+ }
1652
+ /** Patch assembled fields without copying inherited fields into the variant's stored payload. */
1653
+ function updateEntityFields(rows, entityId, fields) {
1654
+ assembleEntityContent(rows, entityId);
1655
+ if (!isJsonObject(fields) || Object.hasOwn(fields, "entityId") || Object.hasOwn(fields, "entityKind")) throw new ScriptCompositionError("invalid_bases", entityId, "Field updates require JSON without reserved identity fields");
1656
+ const updates = /* @__PURE__ */ new Map();
1657
+ for (const [field, value] of Object.entries(fields)) {
1658
+ const ownerId = resolveEntityFieldOwner(rows, entityId, field);
1659
+ const owner = updates.get(ownerId) ?? rows.entities.find((row) => row.entityId === ownerId);
1660
+ updates.set(ownerId, {
1661
+ ...owner,
1662
+ payload: {
1663
+ ...owner.payload,
1664
+ [field]: value
1665
+ }
1666
+ });
1667
+ }
1668
+ return [...updates.values()].map((row) => JSON.parse(JSON.stringify(row)));
1669
+ }
1670
+ /** Find the actual AudioScript text owner through direct variant bases. */
1671
+ function findComposedAudioScript(rows, variantEntityId, variantKind) {
1672
+ const variant = requireEntityKind(rows, variantEntityId, variantKind);
1673
+ assembleEntityContent(rows, variantEntityId);
1674
+ const sources = /* @__PURE__ */ new Map();
1675
+ const seen = /* @__PURE__ */ new Set();
1676
+ const visit = (row) => {
1677
+ for (const id of variantBaseEntityIds(row)) {
1678
+ if (seen.has(id)) continue;
1679
+ seen.add(id);
1680
+ const base = rows.entities.find((candidate) => candidate.entityId === id);
1681
+ if (base.entityKind === "audio-script") sources.set(id, assembleEntityContent(rows, id));
1682
+ else visit(base);
1683
+ }
1684
+ };
1685
+ visit(variant);
1686
+ if (sources.size !== 1) throw new ScriptCompositionError(sources.size === 0 ? "composition_missing" : "composition_ambiguous", variantEntityId, `${variantKind} "${variantEntityId}" baseEntityIds must resolve one AudioScript text owner, got ${sources.size}`);
1687
+ return [...sources.values()][0];
1688
+ }
1689
+ /** Join the whole AudioScript text in segment order. */
1690
+ function assembleScriptText(script) {
1691
+ return scriptSegments(script).map((segment) => segment.text).join("");
1692
+ }
1693
+ function scriptSegments(script) {
1694
+ const segments = script.payload.segments;
1695
+ if (!Array.isArray(segments)) throw new ScriptCompositionError("composition_dangling", script.entityId, `AudioScript "${script.entityId}" requires segments`);
1696
+ if (!segments.every(isScriptSegment) || new Set(segments.map((s) => s.segmentId)).size !== segments.length) throw new ScriptCompositionError("invalid_script", script.entityId, "AudioScript requires valid segments with unique local segmentId and text");
1697
+ return segments;
1698
+ }
1699
+ function isScriptSegment(value) {
1700
+ if (typeof value !== "object" || value == null || Array.isArray(value)) return false;
1701
+ const segment = value;
1702
+ return typeof segment.segmentId === "string" && segment.segmentId.trim() !== "" && typeof segment.text === "string" && (segment.language === void 0 || typeof segment.language === "string");
1703
+ }
1704
+ /** Assemble selected text after base-field validation and explicit variant overrides. */
1705
+ function assembleCaptionContent(rows, captionEntityId) {
1706
+ requireEntityKind(rows, captionEntityId, "caption");
1707
+ const caption = assembleEntityContent(rows, captionEntityId);
1708
+ const script = findComposedAudioScript(rows, captionEntityId, "caption");
1709
+ const segment = selectAudioScriptSegment({
1710
+ ...script,
1711
+ payload: caption.payload
1712
+ }, captionSelection(caption));
1713
+ return {
1714
+ caption,
1715
+ audioScript: script,
1716
+ segments: [segment],
1717
+ text: segment.text
1718
+ };
1719
+ }
1720
+ /**
1721
+ * Assemble the complete Phonetic Script content for TTS: the composed
1722
+ * AudioScript's base text plus the variant's own phoneme and prosody fields.
1723
+ */
1724
+ function assemblePhoneticScriptContent(rows, phoneticScriptEntityId) {
1725
+ requireEntityKind(rows, phoneticScriptEntityId, "phonetic-script");
1726
+ const phoneticScript = assembleEntityContent(rows, phoneticScriptEntityId);
1727
+ const audioScript = findComposedAudioScript(rows, phoneticScriptEntityId, "phonetic-script");
1728
+ const segments = scriptSegments({
1729
+ ...audioScript,
1730
+ payload: phoneticScript.payload
1731
+ });
1732
+ return {
1733
+ phoneticScript,
1734
+ audioScript,
1735
+ segments,
1736
+ text: segments.map((segment) => segment.text).join("")
1737
+ };
1738
+ }
1739
+ function captionSelection(caption) {
1740
+ if (Object.hasOwn(caption.payload, "selections") || !isSegmentSelection(caption.payload.selection)) throw new ScriptCompositionError("invalid_selection", caption.entityId, "Caption requires one selection object with segmentId and optional textRange; selections arrays are forbidden");
1741
+ return caption.payload.selection;
1742
+ }
1743
+ function isSegmentSelection(value) {
1744
+ return typeof value === "object" && value != null && !Array.isArray(value) && typeof value.segmentId === "string" && value.segmentId.trim() !== "" && Object.keys(value).every((key) => key === "segmentId" || key === "textRange");
1745
+ }
1746
+ /** Select source text without creating another authoritative text field. */
1747
+ function selectAudioScriptSegment(script, selection) {
1748
+ if (!isSegmentSelection(selection)) throw new ScriptCompositionError("invalid_selection", script.entityId, "Caption requires one selection object with segmentId and optional textRange");
1749
+ const bySegmentId = new Map(scriptSegments(script).map((segment) => [segment.segmentId, segment]));
1750
+ const selected = (() => {
1751
+ const segment = bySegmentId.get(selection.segmentId);
1752
+ if (segment === void 0) throw new ScriptCompositionError("unknown_segment", script.entityId, `Caption selection "${selection.segmentId}" does not name a segment of AudioScript "${script.entityId}"`);
1753
+ if (selection.textRange === void 0) return segment;
1754
+ const range = selection.textRange;
1755
+ const points = Array.from(segment.text);
1756
+ if (typeof range !== "object" || range === null || !Number.isSafeInteger(range.start) || !Number.isSafeInteger(range.end) || range.start < 0 || range.end <= range.start || range.end > points.length || Object.keys(range).some((key) => key !== "start" && key !== "end")) throw new ScriptCompositionError("invalid_selection", script.entityId, "Caption textRange must be a non-empty half-open Unicode code-point range within its source segment");
1757
+ return {
1758
+ ...segment,
1759
+ text: points.slice(range.start, range.end).join("")
1760
+ };
1761
+ })();
1762
+ if (!selected.text.trim()) throw new ScriptCompositionError("empty_selection", script.entityId, "Caption selection must contain visible source text");
1763
+ return selected;
1764
+ }
1765
+ function requireEntityKind(rows, entityIdValue, entityKind) {
1766
+ const entity = rows.entities.find((candidate) => candidate.entityId === entityIdValue);
1767
+ if (entity === void 0) throw new ScriptCompositionError("composition_dangling", entityIdValue, `Entity "${entityIdValue}" does not exist`);
1768
+ if (entity.entityKind !== entityKind) throw new ScriptCompositionError("composition_dangling", entityIdValue, `Entity "${entityIdValue}" must have kind "${entityKind}", got "${entity.entityKind}"`);
1769
+ return entity;
1770
+ }
1771
+ //#endregion
1772
+ //#region ../medeo-dsl/src/rows.ts
1773
+ function entityToRow(entity) {
1774
+ const { entityId, entityKind, ...payload } = entity;
1775
+ if (!isJsonObject(payload)) throw new Error(`Entity "${entityId}" payload must contain only JSON values`);
1776
+ return {
1777
+ entityId,
1778
+ entityKind,
1779
+ payload
1780
+ };
1781
+ }
1782
+ //#endregion
1783
+ //#region ../medeo-dsl/src/invariants.ts
1784
+ /**
1785
+ * Media variants own their Asset identity directly: a `physical-asset`
1786
+ * Relation may not bind them. Caption may associate with a physical text Asset
1787
+ * independently of its AudioScript composition.
1788
+ */
1789
+ function validateMediaAssetIdentity(entity, index) {
1790
+ return [...index.relationsOf(entity)].filter((relation) => relation.kind === "physical-asset").map(() => ({
1791
+ code: "media_physical_asset_forbidden",
1792
+ entityId: entity.entityId,
1793
+ message: `Media entity "${entity.entityId}" owns its Asset identity; remove its physical-asset Relation`
1794
+ }));
1795
+ }
1796
+ /** Validates one complete set of entities and its authoritative Relation rows. */
1797
+ function validateEntityRelationSet(entityRefs, index, options) {
1798
+ const { entities, issues } = collectRelatedEntities(entityRefs, index);
1799
+ const entityIds = new Set(entities.map((entity) => entity.entityId));
1800
+ const rows = {
1801
+ entities: entities.map((entity) => entityToRow(entity.current())),
1802
+ relations: []
1803
+ };
1804
+ for (const entity of entities) try {
1805
+ assembleEntityContent(rows, entity.entityId);
1806
+ } catch (error) {
1807
+ issues.push({
1808
+ code: "invalid_variant_composition",
1809
+ entityId: entity.entityId,
1810
+ message: error instanceof Error ? error.message : "Invalid variant composition"
1811
+ });
1812
+ }
1813
+ for (const entity of entities) {
1814
+ const current = entity.current();
1815
+ const entityIssues = validateEntity(entity, entityIds);
1816
+ issues.push(...entityIssues);
1817
+ if (isMediaAssetVariantKind(current.entityKind)) issues.push(...validateMediaAssetIdentity(entity, index));
1818
+ if (entityIssues.some((issue) => issue.code === "invalid_entity_payload")) continue;
1819
+ if (current.entityKind === "sequence-marker") {
1820
+ const marker = entity;
1821
+ issues.push(...validateMarkerUse(entity, index));
1822
+ issues.push(...validateMarkerRanges(marker, {
1823
+ source: (left, right) => options.compareMarkerPoints(marker, "source", left, right),
1824
+ target: (left, right) => options.compareMarkerPoints(marker, "target", left, right)
1825
+ }));
1826
+ issues.push(...validateMarkerSourceBounds(marker, index, (left, right) => options.compareMarkerPoints(marker, "source", left, right)));
1827
+ }
1828
+ if (current.entityKind === "clip") {
1829
+ issues.push(...validateClipAdmission(entity, index));
1830
+ issues.push(...validateClipPlacement(entity, index));
1831
+ }
1832
+ if (current.entityKind === "axvideo") issues.push(...validateAXVideoAdmission(entity, index));
1833
+ if (current.entityKind === "caption") issues.push(...validateCaptionComposition(entity, index, entities));
1834
+ if (current.entityKind === "phonetic-script") issues.push(...validatePhoneticComposition(entity, index, entities));
1835
+ issues.push(...validateSequenceComposition(entity));
1836
+ }
1837
+ issues.push(...validateClipAnchorCycles(entities, index));
1838
+ return issues;
1839
+ }
1840
+ function validateMarkerUse(marker, index) {
1841
+ const relations = [...index.relationsOf(marker)];
1842
+ const clipEdges = ofKind(relations, "clip-marker");
1843
+ const axVideoEdges = ofKind(relations, "axvideo-marker");
1844
+ const contentEdges = ofKind(relations, "marker-content");
1845
+ const timelineEdges = ofKind(relations, "marker-timeline");
1846
+ const scriptEdges = ofKind(relations, "audio-script-marker");
1847
+ const issues = [];
1848
+ if (scriptEdges.length > 0) {
1849
+ if (clipEdges.length + axVideoEdges.length + contentEdges.length + timelineEdges.length > 0) issues.push({
1850
+ code: "marker_container_xor",
1851
+ entityId: marker.entityId,
1852
+ message: "An AudioScript annotation Marker must not carry Clip, AXVideo, content, or Timeline relations"
1853
+ });
1854
+ const ranges = marker.current().segmentRanges;
1855
+ if (!ranges?.length) issues.push({
1856
+ code: "invalid_entity_payload",
1857
+ entityId: marker.entityId,
1858
+ message: "An AudioScript annotation Marker requires segmentRanges"
1859
+ });
1860
+ for (const relation of scriptEdges) {
1861
+ const script = relation.other(marker)?.deref()?.current();
1862
+ if (script?.entityKind !== "audio-script") continue;
1863
+ const segments = script.segments;
1864
+ if (!Array.isArray(segments)) continue;
1865
+ const segmentIds = new Set(segments.map((segment) => segment.segmentId));
1866
+ if (ranges?.some((range) => !segmentIds.has(range.segmentId))) issues.push({
1867
+ code: "invalid_entity_payload",
1868
+ entityId: marker.entityId,
1869
+ message: "Annotation segmentRanges must name segments of the related AudioScript"
1870
+ });
1871
+ }
1872
+ return issues;
1873
+ }
1874
+ if (marker.current().segmentRanges !== void 0) issues.push({
1875
+ code: "invalid_entity_payload",
1876
+ entityId: marker.entityId,
1877
+ message: "segmentRanges belongs to an AudioScript annotation Marker, not a display Marker"
1878
+ });
1879
+ if (clipEdges.length + axVideoEdges.length !== 1) issues.push({
1880
+ code: "marker_container_xor",
1881
+ entityId: marker.entityId,
1882
+ message: "Sequence Marker must have exactly one Clip XOR AXVideo container relation"
1883
+ });
1884
+ if (contentEdges.length + timelineEdges.length !== 1) issues.push({
1885
+ code: "marker_content_xor",
1886
+ entityId: marker.entityId,
1887
+ message: "Sequence Marker must have exactly one Sequence content XOR Timeline relation"
1888
+ });
1889
+ if (clipEdges.length === 1 && timelineEdges.length === 1 || axVideoEdges.length === 1 && contentEdges.length === 1) issues.push({
1890
+ code: "marker_pair_mismatch",
1891
+ entityId: marker.entityId,
1892
+ message: "Only Clip+Content or AXVideo+Timeline Marker relation pairs are valid"
1893
+ });
1894
+ return issues;
1895
+ }
1896
+ function validateClipAdmission(clip, index) {
1897
+ const markerEdges = ofKind([...index.relationsOf(clip)], "clip-marker");
1898
+ if (markerEdges.length !== 1) return [{
1899
+ code: "clip_marker_cardinality",
1900
+ entityId: clip.entityId,
1901
+ message: "Clip must have exactly one authoritative Clip-Marker Relation"
1902
+ }];
1903
+ const marker = markerEdges[0]?.other(clip)?.deref();
1904
+ if (marker == null) return [{
1905
+ code: "clip_content_cardinality",
1906
+ entityId: clip.entityId,
1907
+ message: "Clip must resolve one live Sequence Marker and one content Relation"
1908
+ }];
1909
+ const contentEdges = ofKind([...index.relationsOf(marker)], "marker-content");
1910
+ if (contentEdges.length !== 1) return [{
1911
+ code: "clip_content_cardinality",
1912
+ entityId: clip.entityId,
1913
+ message: "Clip Marker must resolve exactly one content Relation"
1914
+ }];
1915
+ const content = contentEdges[0]?.other(marker)?.deref()?.current();
1916
+ if (content == null) return [{
1917
+ code: "clip_content_cardinality",
1918
+ entityId: clip.entityId,
1919
+ message: "Clip Marker content Relation must resolve one live entity"
1920
+ }];
1921
+ if (!hasSequence(content)) return [{
1922
+ code: "clip_content_not_sequence",
1923
+ entityId: clip.entityId,
1924
+ message: `Clip Marker resolves to non-Sequence entity "${content.entityId}"`
1925
+ }];
1926
+ return [];
1927
+ }
1928
+ function validateAXVideoAdmission(axVideo, index) {
1929
+ const markerEdges = ofKind([...index.relationsOf(axVideo)], "axvideo-marker");
1930
+ if (markerEdges.length === 1 && markerEdges[0]?.other(axVideo)?.deref() != null) return [];
1931
+ return [{
1932
+ code: "axvideo_marker_cardinality",
1933
+ entityId: axVideo.entityId,
1934
+ message: "AXVideo must have exactly one live AXVideo-Marker Relation"
1935
+ }];
1936
+ }
1937
+ /** Validate a Caption against the complete entity set, including its directly held bases. */
1938
+ function validateCaptionComposition(caption, index, entities = []) {
1939
+ return validateVariantComposition(caption, index, entities, "caption");
1940
+ }
1941
+ function validatePhoneticComposition(phonetic, index, entities = []) {
1942
+ return validateVariantComposition(phonetic, index, entities, "phonetic-script");
1943
+ }
1944
+ function validateVariantComposition(variant, index, entities, kind) {
1945
+ const issues = [];
1946
+ const rows = {
1947
+ entities: collectRelatedEntities([...entities, variant], index).entities.map((ref) => entityToRow(ref.current())),
1948
+ relations: []
1949
+ };
1950
+ try {
1951
+ if (kind === "caption") assembleCaptionContent(rows, variant.entityId);
1952
+ else assemblePhoneticScriptContent(rows, variant.entityId);
1953
+ } catch (error) {
1954
+ issues.push({
1955
+ code: kind === "caption" ? "caption_composition_required" : "phonetic_script_composition_required",
1956
+ entityId: variant.entityId,
1957
+ message: error instanceof Error ? error.message : "Invalid variant composition"
1958
+ });
1959
+ }
1960
+ return issues;
1961
+ }
1962
+ function validateClipPlacement(clip, index) {
1963
+ const marker = ofKind([...index.relationsOf(clip)], "clip-marker")[0]?.other(clip)?.deref();
1964
+ if (marker?.current().entityKind !== "sequence-marker") return [];
1965
+ const markerValue = marker.current();
1966
+ const anchors = ofKind([...index.relationsOf(clip)], "clip-anchor").filter((relation) => relation.endpoints[0].deref() === clip);
1967
+ const hasOrder = clip.current().order !== void 0;
1968
+ const hasTarget = markerValue.targetRange !== void 0;
1969
+ const hasAnchorOffset = markerValue.anchorOffset !== void 0;
1970
+ const issues = [];
1971
+ if (anchors.length > 1) issues.push({
1972
+ code: "clip_anchor_cardinality",
1973
+ entityId: clip.entityId,
1974
+ message: "A Clip may follow at most one host Clip"
1975
+ });
1976
+ const hasAnchor = anchors.length === 1;
1977
+ if (hasAnchor && (!hasAnchorOffset || hasOrder || hasTarget) || !hasAnchor && (hasAnchorOffset || Number(hasOrder) + Number(hasTarget) !== 1)) issues.push({
1978
+ code: "clip_placement_invalid",
1979
+ entityId: clip.entityId,
1980
+ message: "Clip placement must be exactly one of Clip.order, Marker.targetRange, or clip-anchor with Marker.anchorOffset"
1981
+ });
1982
+ if (markerValue.durationPolicy === "timeline") {
1983
+ const content = ofKind([...index.relationsOf(marker)], "marker-content")[0]?.other(marker)?.deref()?.current();
1984
+ const track = ofKind([...index.relationsOf(clip)], "track-clip")[0]?.other(clip)?.deref()?.current();
1985
+ const trackRole = track?.entityKind === "track" ? track.role : void 0;
1986
+ if (content?.entityKind !== "audio" || trackRole !== "bgm" || !hasOrder && !hasTarget) issues.push({
1987
+ code: "timeline_duration_policy_invalid",
1988
+ entityId: marker.entityId,
1989
+ message: "Marker durationPolicy \"timeline\" is only valid for ordered or absolutely placed Audio Clips on the bgm Track"
1990
+ });
1991
+ }
1992
+ return issues;
1993
+ }
1994
+ function validateClipAnchorCycles(entities, index) {
1995
+ const hostByChild = /* @__PURE__ */ new Map();
1996
+ for (const entity of entities) {
1997
+ if (entity.current().entityKind !== "clip") continue;
1998
+ for (const relation of ofKind([...index.relationsOf(entity)], "clip-anchor")) {
1999
+ if (relation.endpoints[0].deref() !== entity) continue;
2000
+ const host = relation.endpoints[1].deref();
2001
+ if (host != null) hostByChild.set(entity.entityId, host.entityId);
2002
+ }
2003
+ }
2004
+ const issues = [];
2005
+ for (const child of hostByChild.keys()) {
2006
+ const seen = /* @__PURE__ */ new Set();
2007
+ let current = child;
2008
+ while (current !== void 0 && !seen.has(current)) {
2009
+ seen.add(current);
2010
+ current = hostByChild.get(current);
2011
+ }
2012
+ if (current === void 0) continue;
2013
+ issues.push({
2014
+ code: "clip_anchor_cycle",
2015
+ entityId: child,
2016
+ message: "clip-anchor Relations must form an acyclic dependency graph"
2017
+ });
2018
+ }
2019
+ return issues;
2020
+ }
2021
+ function validateMarkerRanges(marker, compare) {
2022
+ const current = marker.current();
2023
+ const issues = [];
2024
+ if (!(compare.source(current.sourceRange.start, current.sourceRange.end) < 0)) issues.push({
2025
+ code: "marker_source_range_empty",
2026
+ entityId: marker.entityId,
2027
+ message: "Sequence Marker sourceRange must be a non-empty half-open interval"
2028
+ });
2029
+ if (current.targetRange != null && !(compare.target(current.targetRange.start, current.targetRange.end) < 0)) issues.push({
2030
+ code: "marker_target_range_empty",
2031
+ entityId: marker.entityId,
2032
+ message: "Sequence Marker targetRange must be a non-empty half-open interval"
2033
+ });
2034
+ return issues;
2035
+ }
2036
+ function validateMarkerSourceBounds(marker, index, compare) {
2037
+ const contentEdges = ofKind([...index.relationsOf(marker)], "marker-content");
2038
+ if (contentEdges.length !== 1) return [];
2039
+ const content = contentEdges[0]?.other(marker)?.deref()?.current();
2040
+ if (content == null || !hasSequence(content)) return [];
2041
+ const sourceRange = marker.current().sourceRange;
2042
+ const startVsExtent = compare(sourceRange.start, content.extent.start);
2043
+ const endVsExtent = content.extent.kind === "bounded" ? compare(sourceRange.end, content.extent.end) : void 0;
2044
+ if (!Number.isNaN(startVsExtent) && startVsExtent >= 0 && (endVsExtent == null || !Number.isNaN(endVsExtent) && endVsExtent <= 0)) return [];
2045
+ return [{
2046
+ code: "marker_source_out_of_bounds",
2047
+ entityId: marker.entityId,
2048
+ message: `Sequence Marker sourceRange must stay within content "${content.entityId}" extent`
2049
+ }];
2050
+ }
2051
+ function validateSequenceComposition(entity) {
2052
+ const current = entity.current();
2053
+ const expected = expectedSequenceShape(current.entityKind);
2054
+ if (expected == null) return [];
2055
+ if (hasSequence(current) && current.extent.kind === expected.extent && current.sampling === expected.sampling) return [];
2056
+ return [{
2057
+ code: "invalid_sequence_composition",
2058
+ entityId: current.entityId,
2059
+ message: `${current.entityKind} must compose ${expected.extent} / ${expected.sampling} Sequence semantics`
2060
+ }];
2061
+ }
2062
+ function validateEntity(entity, entityIds = /* @__PURE__ */ new Set()) {
2063
+ const current = entity.current();
2064
+ const issues = [];
2065
+ if (isReservedEntityKind(current.entityKind)) issues.push({
2066
+ code: "forbidden_entity_kind",
2067
+ entityId: current.entityId,
2068
+ message: `Entity kind "${current.entityKind}" is explicitly outside the Medeo DSL`
2069
+ });
2070
+ for (const problem of validateKnownEntityPayload(current)) issues.push({
2071
+ code: "invalid_entity_payload",
2072
+ entityId: current.entityId,
2073
+ message: `Entity "${current.entityKind}" ${problem}`
2074
+ });
2075
+ const peerIdPaths = [...collectPeerEntityIdPaths(current, current.entityKind), ...collectPeerEntityValuePaths(current, current.entityKind, current.entityId, entityIds)];
2076
+ const uniquePeerIdPaths = [...new Set(peerIdPaths)].sort();
2077
+ if (uniquePeerIdPaths.length > 0) issues.push({
2078
+ code: "peer_entity_id_field",
2079
+ entityId: current.entityId,
2080
+ message: `Entity embeds forbidden peer-ID field/value path(s): ${uniquePeerIdPaths.join(", ")}`
2081
+ });
2082
+ return issues;
2083
+ }
2084
+ function validateKnownEntityPayload(entity) {
2085
+ const value = entity;
2086
+ const problems = [];
2087
+ if (value.lifecycle !== void 0 && !isRecord(value.lifecycle)) problems.push("lifecycle must be an object when present");
2088
+ if (entity.entityKind === "audio-script" || entity.entityKind === "phonetic-script") {
2089
+ for (const key of [
2090
+ "extent",
2091
+ "sampling",
2092
+ "coordinateSpace",
2093
+ "sourceRange",
2094
+ "targetRange",
2095
+ "duration",
2096
+ "startMs",
2097
+ "endMs"
2098
+ ]) if (Object.hasOwn(value, key)) problems.push(`${key} is not intrinsic script data; assign an external annotation Marker`);
2099
+ }
2100
+ switch (entity.entityKind) {
2101
+ case "track":
2102
+ validateOptionalField(value, "hidden", "boolean", problems);
2103
+ validateOptionalField(value, "role", "string", problems);
2104
+ validateOptionalFiniteNumber(value, "order", problems);
2105
+ break;
2106
+ case "video":
2107
+ case "audio":
2108
+ case "voice":
2109
+ case "caption":
2110
+ validateSequencePayload(value, "bounded", "native", problems);
2111
+ if (entity.entityKind !== "caption") validateMediaAssetFields(value, problems);
2112
+ if (entity.entityKind === "voice") validateVoicePayload(value, problems);
2113
+ if (entity.entityKind === "caption") validateCaptionPayload(value, problems);
2114
+ break;
2115
+ case "image":
2116
+ validateSequencePayload(value, "unbounded", "constant", problems);
2117
+ validateMediaAssetFields(value, problems);
2118
+ break;
2119
+ case "axvideo":
2120
+ validateSequencePayload(value, "bounded", "derived", problems);
2121
+ break;
2122
+ case "sequence-marker":
2123
+ validateMarkerPayload(value, problems);
2124
+ break;
2125
+ case "audio-script":
2126
+ validateScriptPayload(value, problems);
2127
+ break;
2128
+ case "phonetic-script":
2129
+ validatePhoneticScriptPayload(value, problems);
2130
+ break;
2131
+ case "timeline":
2132
+ case "viewport": break;
2133
+ case "clip":
2134
+ validateOptionalFiniteNumber(value, "order", problems);
2135
+ validateOptionalFiniteNumber(value, "volume", problems);
2136
+ if (typeof value.volume === "number" && (value.volume < -60 || value.volume > 20)) problems.push("volume must be decibels between -60 and 20");
2137
+ break;
2138
+ case "asset":
2139
+ validateAssetPayload(value, problems);
2140
+ break;
2141
+ default: break;
2142
+ }
2143
+ return problems;
2144
+ }
2145
+ function validateSequencePayload(value, expectedExtent, expectedSampling, problems) {
2146
+ const extent = value.extent;
2147
+ if (!isRecord(extent)) problems.push("extent must be an object");
2148
+ else {
2149
+ if (extent.kind !== expectedExtent) problems.push(`extent.kind must be "${expectedExtent}"`);
2150
+ if (!Object.hasOwn(extent, "start") || extent.start === void 0) problems.push("extent.start is required");
2151
+ if (expectedExtent === "bounded" && (!Object.hasOwn(extent, "end") || extent.end === void 0)) problems.push("extent.end is required for bounded sequences");
2152
+ if (expectedExtent === "unbounded" && Object.hasOwn(extent, "end")) problems.push("extent.end is forbidden for unbounded sequences");
2153
+ }
2154
+ if (value.sampling !== expectedSampling) problems.push(`sampling must be "${expectedSampling}"`);
2155
+ if (!Object.hasOwn(value, "coordinateSpace") || value.coordinateSpace === void 0) problems.push("coordinateSpace is required");
2156
+ }
2157
+ function validateMarkerPayload(value, problems) {
2158
+ validateRange(value.sourceRange, "sourceRange", true, problems);
2159
+ if (Object.hasOwn(value, "targetRange") && value.targetRange !== void 0) validateRange(value.targetRange, "targetRange", true, problems);
2160
+ const duration = value.duration;
2161
+ if (!isRecord(duration)) problems.push("duration must be an object");
2162
+ else if (duration.mode === "fixed") {
2163
+ if (!Object.hasOwn(duration, "value") || duration.value === void 0) problems.push("duration.value is required when duration.mode is \"fixed\"");
2164
+ } else if (duration.mode !== "from-source") problems.push("duration.mode must be \"from-source\" or \"fixed\"");
2165
+ if (value.anchorOffset === void 0 && Object.hasOwn(value, "anchorOffset")) problems.push("anchorOffset cannot be undefined when present");
2166
+ if (value.durationPolicy !== void 0 && value.durationPolicy !== "timeline") problems.push("durationPolicy must be \"timeline\" when present");
2167
+ if (value.segmentRanges !== void 0) validateSegmentRanges(value.segmentRanges, problems);
2168
+ }
2169
+ /** Annotation Marker time values are directly assigned facts; no cross-references are allowed. */
2170
+ function validateSegmentRanges(value, problems) {
2171
+ if (!Array.isArray(value)) {
2172
+ problems.push("segmentRanges must be an array when present");
2173
+ return;
2174
+ }
2175
+ const seen = /* @__PURE__ */ new Set();
2176
+ for (const [index, entry] of value.entries()) {
2177
+ if (!isRecord(entry)) {
2178
+ problems.push(`segmentRanges[${index}] must be an object`);
2179
+ continue;
2180
+ }
2181
+ if (typeof entry.segmentId !== "string") problems.push(`segmentRanges[${index}].segmentId must be a string`);
2182
+ else if (seen.has(entry.segmentId)) problems.push(`segmentRanges[${index}].segmentId must be unique within the Marker`);
2183
+ else seen.add(entry.segmentId);
2184
+ for (const key of ["startMs", "endMs"]) {
2185
+ const point = entry[key];
2186
+ if (typeof point !== "number" || !Number.isFinite(point)) problems.push(`segmentRanges[${index}].${key} must be a finite number`);
2187
+ }
2188
+ if (typeof entry.startMs === "number" && Number.isFinite(entry.startMs) && typeof entry.endMs === "number" && Number.isFinite(entry.endMs) && entry.startMs > entry.endMs) problems.push(`segmentRanges[${index}].startMs must not exceed segmentRanges[${index}].endMs`);
2189
+ }
2190
+ }
2191
+ function validateAssetPayload(value, problems) {
2192
+ validateExternalLocator(value, problems);
2193
+ validateStorageKey(value, problems);
2194
+ if (value.inline !== void 0) problems.push("inline is not a physical location; text is domain content owned by its AudioScript");
2195
+ }
2196
+ /**
2197
+ * Media variants own their Asset locator directly: `external` is required and
2198
+ * textual domain content is owned by AudioScript; the physical-location
2199
+ * vocabularies cannot mix.
2200
+ */
2201
+ function validateMediaAssetFields(value, problems) {
2202
+ if (value.external === void 0) problems.push("external is required; media variants own their Asset identity");
2203
+ else validateExternalLocator(value, problems);
2204
+ validateStorageKey(value, problems);
2205
+ if (value.inline !== void 0) problems.push("media variants use external locators, not inline domain text");
2206
+ }
2207
+ function validateExternalLocator(value, problems) {
2208
+ const external = value.external;
2209
+ if (external === void 0) return;
2210
+ if (!isRecord(external)) {
2211
+ problems.push("external must be an object when present");
2212
+ return;
2213
+ }
2214
+ if (external.system !== "memota" && external.system !== "memota-speech") problems.push("external.system must be \"memota\" or \"memota-speech\"");
2215
+ if (typeof external.key !== "string" || external.key.trim() === "") problems.push("external.key must be a non-empty string");
2216
+ }
2217
+ function validateStorageKey(value, problems) {
2218
+ if (value.storageKey !== void 0 && (typeof value.storageKey !== "string" || value.storageKey.trim() === "")) problems.push("storageKey must be a non-empty string when present");
2219
+ }
2220
+ function validateVoicePayload(value, problems) {
2221
+ const voice = value.voice;
2222
+ if (voice === void 0) return;
2223
+ if (!isRecord(voice)) {
2224
+ problems.push("voice must be an object when present");
2225
+ return;
2226
+ }
2227
+ if (voice.system !== "voice-library") problems.push("voice.system must be \"voice-library\"");
2228
+ if (typeof voice.key !== "string" || voice.key.trim() === "") problems.push("voice.key must be a non-empty string");
2229
+ if (voice.name !== void 0 && typeof voice.name !== "string") problems.push("voice.name must be a string when present");
2230
+ }
2231
+ function validateCaptionPayload(value, problems) {
2232
+ validateExternalLocator(value, problems);
2233
+ if (value.segmentRanges !== void 0) validateSegmentRanges(value.segmentRanges, problems);
2234
+ if (Object.hasOwn(value, "selections")) problems.push("selections is forbidden; Caption requires one selection object");
2235
+ const selection = value.selection;
2236
+ if (!isRecord(selection)) problems.push("selection must be one AudioScript segment selection object, not an array");
2237
+ else {
2238
+ if (typeof selection.segmentId !== "string" || !selection.segmentId.trim()) problems.push("selection.segmentId must be a non-empty string");
2239
+ if (Object.keys(selection).some((key) => key !== "segmentId" && key !== "textRange")) problems.push("selection may only contain segmentId and textRange");
2240
+ const range = selection.textRange;
2241
+ if (range !== void 0 && (!isRecord(range) || !Number.isSafeInteger(range.start) || !Number.isSafeInteger(range.end) || range.start < 0 || range.end <= range.start || Object.keys(range).some((key) => key !== "start" && key !== "end"))) problems.push("selection.textRange must be a non-empty half-open code-point range");
2242
+ if (Array.isArray(value.segmentRanges) && (value.segmentRanges.length !== 1 || !isRecord(value.segmentRanges[0]) || value.segmentRanges[0].segmentId !== selection.segmentId)) problems.push("Caption segmentRanges must contain only the selected segment timing");
2243
+ }
2244
+ const style = value.style;
2245
+ if (style === void 0) return;
2246
+ if (!isRecord(style)) {
2247
+ problems.push("style must be an object when present");
2248
+ return;
2249
+ }
2250
+ const font = style.font;
2251
+ if (font !== void 0) if (!isRecord(font)) problems.push("style.font must be an object when present");
2252
+ else {
2253
+ if (font.system !== "font-library") problems.push("style.font.system must be \"font-library\"");
2254
+ if (typeof font.key !== "string" || font.key.trim() === "") problems.push("style.font.key must be a non-empty string");
2255
+ }
2256
+ for (const key of [
2257
+ "fontSize",
2258
+ "fontWeight",
2259
+ "entranceAnimationDurationMs",
2260
+ "strokeWidth",
2261
+ "positionX",
2262
+ "positionY"
2263
+ ]) validateOptionalFiniteNumber(style, key, problems, `style.${key}`);
2264
+ for (const key of [
2265
+ "fontColor",
2266
+ "entranceAnimation",
2267
+ "strokeColor"
2268
+ ]) if (style[key] !== void 0 && typeof style[key] !== "string") problems.push(`style.${key} must be a string`);
2269
+ }
2270
+ function validateRange(value, path, required, problems) {
2271
+ if (!isRecord(value)) {
2272
+ if (required) problems.push(`${path} must be an object`);
2273
+ return;
2274
+ }
2275
+ if (!Object.hasOwn(value, "start") || value.start === void 0) problems.push(`${path}.start is required`);
2276
+ if (!Object.hasOwn(value, "end") || value.end === void 0) problems.push(`${path}.end is required`);
2277
+ }
2278
+ /** Phonetic variants store pronunciation fields only; base text stays in the AudioScript. */
2279
+ function validatePhoneticScriptPayload(value, problems) {
2280
+ if (value.segments !== void 0) validateScriptPayload(value, problems);
2281
+ if (value.phonemeScript !== void 0 && (typeof value.phonemeScript !== "string" || value.phonemeScript.trim() === "")) problems.push("phonemeScript must be a non-empty string when present");
2282
+ if (value.prosody !== void 0 && !isRecord(value.prosody)) problems.push("prosody must be an object when present");
2283
+ }
2284
+ function validateScriptPayload(value, problems) {
2285
+ if (!Array.isArray(value.segments)) {
2286
+ problems.push("segments must be an array");
2287
+ return;
2288
+ }
2289
+ for (const [index, segment] of value.segments.entries()) {
2290
+ if (!isRecord(segment)) {
2291
+ problems.push(`segments[${index}] must be an object`);
2292
+ continue;
2293
+ }
2294
+ for (const key of [
2295
+ "startMs",
2296
+ "endMs",
2297
+ "start_ms",
2298
+ "end_ms",
2299
+ "sourceRange",
2300
+ "targetRange",
2301
+ "duration"
2302
+ ]) if (Object.hasOwn(segment, key)) problems.push(`segments[${index}].${key} is timing; attach an AudioScript annotation Marker instead`);
2303
+ if (typeof segment.segmentId !== "string") problems.push(`segments[${index}].segmentId must be a string`);
2304
+ if (typeof segment.text !== "string") problems.push(`segments[${index}].text must be a string`);
2305
+ if (segment.language !== void 0 && typeof segment.language !== "string") problems.push(`segments[${index}].language must be a string when present`);
2306
+ }
2307
+ }
2308
+ function validateOptionalField(value, key, expectedType, problems) {
2309
+ if (value[key] !== void 0 && typeof value[key] !== expectedType) problems.push(`${key} must be a ${expectedType} when present`);
2310
+ }
2311
+ function validateOptionalFiniteNumber(value, key, problems, label = key) {
2312
+ if (value[key] !== void 0 && (typeof value[key] !== "number" || !Number.isFinite(value[key]))) problems.push(`${label} must be a finite number when present`);
2313
+ }
2314
+ function collectPeerEntityIdPaths(value, entityKind) {
2315
+ const paths = [];
2316
+ visitPeerEntityIdPaths(value, entityKind, "", /* @__PURE__ */ new Set(), paths);
2317
+ return paths;
2318
+ }
2319
+ function visitPeerEntityIdPaths(value, entityKind, parentPath, ancestors, paths) {
2320
+ if (typeof value !== "object" || value == null) return;
2321
+ if (ancestors.has(value)) return;
2322
+ ancestors.add(value);
2323
+ if (Array.isArray(value)) for (const [index, item] of value.entries()) visitPeerEntityIdPaths(item, entityKind, `${parentPath}[${index}]`, ancestors, paths);
2324
+ else for (const [key, child] of Object.entries(value)) {
2325
+ const path = parentPath.length === 0 ? key : `${parentPath}.${key}`;
2326
+ if (parentPath.length === 0 && key === "baseEntityIds") continue;
2327
+ if (!(parentPath.length === 0 && key === "entityId") && !isOwnedLocalIdPath(entityKind, path) && isEntityIdFieldName(key)) paths.push(path);
2328
+ visitPeerEntityIdPaths(child, entityKind, path, ancestors, paths);
2329
+ }
2330
+ ancestors.delete(value);
2331
+ }
2332
+ function collectPeerEntityValuePaths(value, entityKind, ownEntityId, entityIds) {
2333
+ const paths = [];
2334
+ visitPeerEntityValues(value, entityKind, ownEntityId, entityIds, "", /* @__PURE__ */ new Set(), paths);
2335
+ return paths;
2336
+ }
2337
+ function visitPeerEntityValues(value, entityKind, ownEntityId, entityIds, path, ancestors, paths) {
2338
+ if (typeof value === "string") {
2339
+ if (value !== ownEntityId && entityIds.has(value) && !isOwnedLocalEntityValuePath(entityKind, path)) paths.push(path);
2340
+ return;
2341
+ }
2342
+ if (typeof value !== "object" || value == null || ancestors.has(value)) return;
2343
+ ancestors.add(value);
2344
+ if (Array.isArray(value)) for (const [index, item] of value.entries()) visitPeerEntityValues(item, entityKind, ownEntityId, entityIds, `${path}[${index}]`, ancestors, paths);
2345
+ else for (const [key, child] of Object.entries(value)) {
2346
+ if (path.length === 0 && (key === "entityId" || key === "entityKind" || key === "baseEntityIds")) continue;
2347
+ visitPeerEntityValues(child, entityKind, ownEntityId, entityIds, path.length === 0 ? key : `${path}.${key}`, ancestors, paths);
2348
+ }
2349
+ ancestors.delete(value);
2350
+ }
2351
+ function isOwnedLocalEntityValuePath(entityKind, path) {
2352
+ if (entityKind === "track" && path === "role") return true;
2353
+ if ([
2354
+ "video",
2355
+ "audio",
2356
+ "voice",
2357
+ "image",
2358
+ "caption",
2359
+ "axvideo"
2360
+ ].includes(entityKind) && /^(?:sampling|coordinateSpace|coordinateSpace\.unit|extent\.kind)$/.test(path)) return true;
2361
+ if (entityKind === "sequence-marker" && /^(?:durationPolicy|duration\.mode|timeRemapping\.(?:kind|mode))$/.test(path)) return true;
2362
+ if ((entityKind === "sequence-marker" || entityKind === "caption") && /^segmentRanges\[\d+\]\.segmentId$/.test(path)) return true;
2363
+ if (entityKind === "asset" && /^(?:external\.(?:system|key)|storageKey)$/.test(path)) return true;
2364
+ if (isMediaAssetVariantKind(entityKind) && /^(?:external\.(?:system|key)|storageKey)$/.test(path)) return true;
2365
+ if (entityKind === "voice" && /^voice\.(?:system|key|name)$/.test(path)) return true;
2366
+ if (entityKind === "caption" && (path.startsWith("style.") || path === "selection.segmentId")) return true;
2367
+ if ([
2368
+ "audio-script",
2369
+ "caption",
2370
+ "phonetic-script"
2371
+ ].includes(entityKind) && /^segments\[\d+\]\.(?:segmentId|text|language)$/.test(path)) return true;
2372
+ return false;
2373
+ }
2374
+ function isOwnedLocalIdPath(entityKind, path) {
2375
+ if (path === "lifecycle.actorId") return true;
2376
+ if ([
2377
+ "audio-script",
2378
+ "caption",
2379
+ "phonetic-script"
2380
+ ].includes(entityKind) && /^segments\[\d+\]\.segmentId$/.test(path)) return true;
2381
+ if (entityKind === "caption" && path === "selection.segmentId") return true;
2382
+ if ((entityKind === "sequence-marker" || entityKind === "caption") && /^segmentRanges\[\d+\]\.segmentId$/.test(path)) return true;
2383
+ if (entityKind === "asset" && /^(?:tracks\[\d+\]\.trackId|renditions\[\d+\]\.renditionId)$/.test(path)) return true;
2384
+ return false;
2385
+ }
2386
+ function isEntityIdFieldName(key) {
2387
+ return key.endsWith("Id") || key.endsWith("Ids") || key.endsWith("ID") || key.endsWith("IDs") || /_ids?$/i.test(key);
2388
+ }
2389
+ function isRecord(value) {
2390
+ return typeof value === "object" && value != null && !Array.isArray(value);
2391
+ }
2392
+ function collectRelatedEntities(entityRefs, index) {
2393
+ const byId = /* @__PURE__ */ new Map();
2394
+ const queue = [...entityRefs];
2395
+ const issues = [];
2396
+ while (queue.length > 0) {
2397
+ const entity = queue.shift();
2398
+ if (entity == null) continue;
2399
+ const existing = byId.get(entity.entityId);
2400
+ if (existing != null) {
2401
+ if (existing !== entity) issues.push({
2402
+ code: "duplicate_entity_id",
2403
+ entityId: entity.entityId,
2404
+ message: `Entity id "${entity.entityId}" has more than one live EntityRef`
2405
+ });
2406
+ continue;
2407
+ }
2408
+ byId.set(entity.entityId, entity);
2409
+ for (const relation of index.relationsOf(entity)) for (const endpoint of relation.endpoints) {
2410
+ const ref = endpoint.deref();
2411
+ if (ref != null && !byId.has(ref.entityId)) queue.push(ref);
2412
+ }
2413
+ }
2414
+ return {
2415
+ entities: [...byId.values()],
2416
+ issues
2417
+ };
2418
+ }
2419
+ function expectedSequenceShape(kind) {
2420
+ if (kind === "video" || kind === "audio" || kind === "voice" || kind === "caption") return {
2421
+ extent: "bounded",
2422
+ sampling: "native"
2423
+ };
2424
+ if (kind === "image") return {
2425
+ extent: "unbounded",
2426
+ sampling: "constant"
2427
+ };
2428
+ if (kind === "axvideo") return {
2429
+ extent: "bounded",
2430
+ sampling: "derived"
2431
+ };
2432
+ }
2433
+ function ofKind(relations, kind) {
2434
+ return relations.filter((relation) => relation.kind === kind);
2435
+ }
2436
+ //#endregion
2437
+ //#region ../medeo-dsl/src/relation-specs.ts
2438
+ const timelineTrackRelationSpec = emptySpec("timeline-track", "timeline", "track");
2439
+ const trackClipRelationSpec = emptySpec("track-clip", "track", "clip");
2440
+ const clipMarkerRelationSpec = emptySpec("clip-marker", "clip", "sequence-marker");
2441
+ const axVideoMarkerRelationSpec = emptySpec("axvideo-marker", "axvideo", "sequence-marker");
2442
+ const markerTimelineRelationSpec = emptySpec("marker-timeline", "sequence-marker", "timeline");
2443
+ const markerContentRelationSpec = Object.freeze({
2444
+ kind: "marker-content",
2445
+ validateEndpoints: (endpoints) => hasMarkerAndSequence(endpoints),
2446
+ validateMetadata: isEmptyMetadata
2447
+ });
2448
+ const physicalAssetRelationSpec = Object.freeze({
2449
+ kind: "physical-asset",
2450
+ validateEndpoints: (endpoints) => hasAssetAndSequence(endpoints),
2451
+ validateMetadata: isAssetBinding
2452
+ });
2453
+ /** `generated(output, input)` means endpoint 0 was generated from endpoint 1. */
2454
+ const generatedRelationSpec = Object.freeze({
2455
+ kind: "generated",
2456
+ validateEndpoints: (endpoints) => endpoints.every((endpoint) => isGeneratedMedia(endpoint.current())),
2457
+ validateMetadata: isEmptyMetadata
2458
+ });
2459
+ const captionAlignmentRelationSpec = Object.freeze({
2460
+ kind: "caption-alignment",
2461
+ validateEndpoints: (endpoints) => hasKinds(endpoints, new Set(["caption"]), new Set(["audio", "voice"])),
2462
+ validateMetadata: isCaptionAlignmentMetadata
2463
+ });
2464
+ /** `clip-anchor(child, host)` means endpoint 0 follows endpoint 1. */
2465
+ const clipAnchorRelationSpec = Object.freeze({
2466
+ kind: "clip-anchor",
2467
+ validateEndpoints: (endpoints) => endpoints[0].current().entityKind === "clip" && endpoints[1].current().entityKind === "clip",
2468
+ validateMetadata: isEmptyMetadata
2469
+ });
2470
+ /** Voice is generated from PhoneticScript; kinds determine roles regardless of endpoint positions. */
2471
+ const phoneticScriptRenderRelationSpec = Object.freeze({
2472
+ kind: "phonetic-script-render",
2473
+ validateEndpoints: (endpoints) => hasKinds(endpoints, new Set(["voice"]), new Set(["phonetic-script"])),
2474
+ validateMetadata: isEmptyMetadata
2475
+ });
2476
+ /** AudioScript was transcribed from Audio, Video, or recorded Voice; kinds determine roles. */
2477
+ const audioScriptSourceRelationSpec = Object.freeze({
2478
+ kind: "audio-script-source",
2479
+ validateEndpoints: (endpoints) => hasKinds(endpoints, new Set(["audio-script"]), new Set([
2480
+ "audio",
2481
+ "video",
2482
+ "voice"
2483
+ ])),
2484
+ validateMetadata: isEmptyMetadata
2485
+ });
2486
+ /**
2487
+ * `audio-script-marker(script, marker)` attaches an annotation Sequence Marker
2488
+ * whose `segmentRanges` hold directly assigned per-Segment time values. The
2489
+ * script keeps no Sequence and no Clip admission; annotation Markers cannot
2490
+ * enter Clip/AXVideo use chains (enforced by the marker-use invariant).
2491
+ */
2492
+ const audioScriptMarkerRelationSpec = Object.freeze({
2493
+ kind: "audio-script-marker",
2494
+ validateEndpoints: (endpoints) => hasKinds(endpoints, new Set(["audio-script"]), new Set(["sequence-marker"])),
2495
+ validateMetadata: isEmptyMetadata
2496
+ });
2497
+ /** Built-in kinds are reserved; callers may add specs only under new names. */
2498
+ const builtInRelationSpecs = Object.freeze([
2499
+ timelineTrackRelationSpec,
2500
+ trackClipRelationSpec,
2501
+ clipMarkerRelationSpec,
2502
+ markerContentRelationSpec,
2503
+ axVideoMarkerRelationSpec,
2504
+ markerTimelineRelationSpec,
2505
+ physicalAssetRelationSpec,
2506
+ generatedRelationSpec,
2507
+ captionAlignmentRelationSpec,
2508
+ clipAnchorRelationSpec,
2509
+ phoneticScriptRenderRelationSpec,
2510
+ audioScriptSourceRelationSpec,
2511
+ audioScriptMarkerRelationSpec
2512
+ ]);
2513
+ function emptySpec(kind, a, b) {
2514
+ return metadataSpec(kind, a, b, isEmptyMetadata);
2515
+ }
2516
+ function metadataSpec(kind, a, b, validateMetadata) {
2517
+ return Object.freeze({
2518
+ kind,
2519
+ validateEndpoints: (endpoints) => hasKinds(endpoints, new Set([a]), new Set([b])),
2520
+ validateMetadata
2521
+ });
2522
+ }
2523
+ function hasKinds(endpoints, aKinds, bKinds) {
2524
+ const first = endpoints[0].current().entityKind;
2525
+ const second = endpoints[1].current().entityKind;
2526
+ return aKinds.has(first) && bKinds.has(second) || aKinds.has(second) && bKinds.has(first);
2527
+ }
2528
+ function hasMarkerAndSequence(endpoints) {
2529
+ const first = endpoints[0].current();
2530
+ const second = endpoints[1].current();
2531
+ return first.entityKind === "sequence-marker" && hasSequence(second) || second.entityKind === "sequence-marker" && hasSequence(first);
2532
+ }
2533
+ function hasAssetAndSequence(endpoints) {
2534
+ const first = endpoints[0].current();
2535
+ const second = endpoints[1].current();
2536
+ return first.entityKind === "asset" && hasSequence(second) || second.entityKind === "asset" && hasSequence(first);
2537
+ }
2538
+ function isGeneratedMedia(entity) {
2539
+ return entity.entityKind === "video" || entity.entityKind === "image" || entity.entityKind === "audio" || entity.entityKind === "voice";
2540
+ }
2541
+ function isEmptyMetadata(value) {
2542
+ return isJsonObject(value) && Object.keys(value).length === 0;
2543
+ }
2544
+ function isAssetBinding(value) {
2545
+ if (!isJsonObject(value)) return false;
2546
+ const orderingKey = /(order|ordinal|position|rank|index|z[_-]?index)/i;
2547
+ return Object.keys(value).every((key) => !orderingKey.test(key));
2548
+ }
2549
+ function isCaptionAlignmentMetadata(value) {
2550
+ return isJsonObject(value) && Object.hasOwn(value, "alignment");
2551
+ }
2552
+ //#endregion
2553
+ //#region ../medeo-dsl/src/relations.ts
2554
+ function createEntityRef(entity) {
2555
+ return {
2556
+ entityId: entity.entityId,
2557
+ current: () => entity
2558
+ };
2559
+ }
2560
+ //#endregion
2561
+ //#region ../medeo-dsl/src/relation-index.ts
2562
+ var RelationEdge = class {
2563
+ relationId;
2564
+ kind;
2565
+ metadata;
2566
+ trace;
2567
+ endpoints;
2568
+ constructor(relationId, kind, endpoints, metadata, trace) {
2569
+ this.relationId = relationId;
2570
+ this.kind = kind;
2571
+ this.metadata = metadata;
2572
+ this.trace = trace;
2573
+ this.endpoints = [new WeakRef(endpoints[0]), new WeakRef(endpoints[1])];
2574
+ }
2575
+ other(entity) {
2576
+ const first = this.endpoints[0].deref();
2577
+ const second = this.endpoints[1].deref();
2578
+ if (first === entity) return this.endpoints[1];
2579
+ if (second === entity) return this.endpoints[0];
2580
+ }
2581
+ toRow() {
2582
+ const first = this.endpoints[0].deref();
2583
+ const second = this.endpoints[1].deref();
2584
+ if (first == null || second == null) return void 0;
2585
+ return {
2586
+ relationId: this.relationId,
2587
+ endpoint0EntityId: first.entityId,
2588
+ endpoint1EntityId: second.entityId,
2589
+ relationKind: this.kind,
2590
+ metadata: this.metadata,
2591
+ trace: this.trace
2592
+ };
2593
+ }
2594
+ isStale() {
2595
+ return this.endpoints[0].deref() == null || this.endpoints[1].deref() == null;
2596
+ }
2597
+ };
2598
+ /** Endpoint-agnostic secondary index. Relation rows remain the persistence authority. */
2599
+ var BiRelationIndex = class {
2600
+ byEntity = /* @__PURE__ */ new WeakMap();
2601
+ canonicalRefs = /* @__PURE__ */ new Map();
2602
+ byRelationId = /* @__PURE__ */ new Map();
2603
+ link(input) {
2604
+ if (input.spec.kind === "generated") throw new Error("Author generated Relations with linkGenerated({ output, input })");
2605
+ if (input.spec.kind === "clip-anchor" || input.spec.kind === "phonetic-script-render" || input.spec.kind === "audio-script-source") throw new Error(`Author ordered ${input.spec.kind} Relations with the dedicated role-named method`);
2606
+ return this.linkValidated(input);
2607
+ }
2608
+ /** Author `generated(output, input)` without exposing positional arguments. */
2609
+ linkGenerated(input) {
2610
+ return this.linkValidated({
2611
+ relationId: input.relationId,
2612
+ spec: generatedRelationSpec,
2613
+ endpoints: [input.output, input.input],
2614
+ metadata: {},
2615
+ trace: input.trace
2616
+ });
2617
+ }
2618
+ /** Author `clip-anchor(child, host)` without exposing positional arguments. */
2619
+ linkClipAnchor(input) {
2620
+ return this.linkValidated({
2621
+ relationId: input.relationId,
2622
+ spec: clipAnchorRelationSpec,
2623
+ endpoints: [input.child, input.host],
2624
+ metadata: {},
2625
+ trace: input.trace
2626
+ });
2627
+ }
2628
+ /** Author `phonetic-script-render(output, phoneticScript)` without exposing positional arguments. */
2629
+ linkPhoneticScriptRender(input) {
2630
+ return this.linkValidated({
2631
+ relationId: input.relationId,
2632
+ spec: phoneticScriptRenderRelationSpec,
2633
+ endpoints: [input.output, input.phoneticScript],
2634
+ metadata: {},
2635
+ trace: input.trace
2636
+ });
2637
+ }
2638
+ /** Author `audio-script-source(script, source)` without exposing positional arguments. */
2639
+ linkAudioScriptSource(input) {
2640
+ return this.linkValidated({
2641
+ relationId: input.relationId,
2642
+ spec: audioScriptSourceRelationSpec,
2643
+ endpoints: [input.script, input.source],
2644
+ metadata: {},
2645
+ trace: input.trace
2646
+ });
2647
+ }
2648
+ /**
2649
+ * Rehydrate a persisted row after resolving its spec and endpoint refs.
2650
+ *
2651
+ * This is a storage-boundary escape hatch, not an authoring API: persisted
2652
+ * positions already are the semantic assertion made by their relation kind.
2653
+ */
2654
+ linkRuntime(input) {
2655
+ return this.linkValidated(input);
2656
+ }
2657
+ linkValidated(input) {
2658
+ const [first, second] = input.endpoints;
2659
+ if (first.entityId === second.entityId) throw new Error("A Relation cannot connect an entity to itself");
2660
+ this.assertRelationIdAvailable(input.relationId);
2661
+ const builtInSpec = builtInSpecByKind.get(input.spec.kind);
2662
+ if (builtInSpec != null && builtInSpec !== input.spec) throw new Error(`Relation kind "${input.spec.kind}" must use its built-in specification`);
2663
+ if (isForbiddenAuthoritativeRelation(input.spec.kind, input.endpoints)) throw new Error("A direct Clip-Content Relation is derived-only and cannot be authoritative");
2664
+ if (!input.spec.validateEndpoints(input.endpoints)) throw new Error(`Relation "${input.spec.kind}" received invalid endpoints`);
2665
+ if (!input.spec.validateMetadata(input.metadata)) throw new Error(`Relation "${input.spec.kind}" received invalid metadata`);
2666
+ this.assertCanonicalRefAvailable(first);
2667
+ this.assertCanonicalRefAvailable(second);
2668
+ const relation = new RelationEdge(input.relationId, input.spec.kind, input.endpoints, input.metadata, input.trace ?? {});
2669
+ this.rememberCanonicalRef(first);
2670
+ this.rememberCanonicalRef(second);
2671
+ this.add(first, relation);
2672
+ this.add(second, relation);
2673
+ this.byRelationId.set(input.relationId, new WeakRef(relation));
2674
+ return relation;
2675
+ }
2676
+ relationsOf(entity) {
2677
+ this.registerCanonicalRef(entity);
2678
+ const relations = this.byEntity.get(entity);
2679
+ if (relations == null) return /* @__PURE__ */ new Set();
2680
+ for (const relation of relations) if (relation instanceof RelationEdge && relation.isStale()) this.unlink(relation);
2681
+ return new Set(relations);
2682
+ }
2683
+ unlink(relation) {
2684
+ for (const endpoint of relation.endpoints) {
2685
+ const ref = endpoint.deref();
2686
+ if (ref != null) this.byEntity.get(ref)?.delete(relation);
2687
+ }
2688
+ if (this.byRelationId.get(relation.relationId)?.deref() === relation) this.byRelationId.delete(relation.relationId);
2689
+ }
2690
+ add(entity, relation) {
2691
+ const relations = this.byEntity.get(entity) ?? /* @__PURE__ */ new Set();
2692
+ relations.add(relation);
2693
+ this.byEntity.set(entity, relations);
2694
+ }
2695
+ registerCanonicalRef(entity) {
2696
+ this.assertCanonicalRefAvailable(entity);
2697
+ this.rememberCanonicalRef(entity);
2698
+ }
2699
+ assertCanonicalRefAvailable(entity) {
2700
+ const existing = this.canonicalRefs.get(entity.entityId)?.deref();
2701
+ if (existing != null && existing !== entity) throw new Error(`Entity "${entity.entityId}" already has a live canonical EntityRef`);
2702
+ }
2703
+ rememberCanonicalRef(entity) {
2704
+ this.canonicalRefs.set(entity.entityId, new WeakRef(entity));
2705
+ }
2706
+ assertRelationIdAvailable(relationId) {
2707
+ if (this.byRelationId.get(relationId)?.deref() != null) throw new Error(`Relation id "${relationId}" already exists`);
2708
+ this.byRelationId.delete(relationId);
2709
+ }
2710
+ };
2711
+ const builtInSpecByKind = new Map(builtInRelationSpecs.map((spec) => [spec.kind, spec]));
2712
+ function isForbiddenAuthoritativeRelation(kind, endpoints) {
2713
+ if (kind === "clip-content") return true;
2714
+ const first = endpoints[0].current();
2715
+ const second = endpoints[1].current();
2716
+ return first.entityKind === "clip" && hasSequence(second) || second.entityKind === "clip" && hasSequence(first);
2717
+ }
2718
+ //#endregion
2719
+ //#region ../medeo-dsl/src/entity-relation-rows.ts
2720
+ var InvalidEntityRelationRowsError = class extends Error {
2721
+ issues;
2722
+ constructor(issues) {
2723
+ super(`Invalid Medeo entity/relation rows:\n- ${issues.join("\n- ")}`);
2724
+ this.issues = issues;
2725
+ this.name = "InvalidEntityRelationRowsError";
2726
+ }
2727
+ };
2728
+ /** Decodes database rows into flat entities and validates the complete relation set. */
2729
+ function decodeEntityRelationRows(rows, options) {
2730
+ const issues = [];
2731
+ const entitiesById = /* @__PURE__ */ new Map();
2732
+ const extensionEntityKinds = new Set(options.entityKinds ?? []);
2733
+ for (const row of rows.entities) {
2734
+ let entity = decodeEntityRow(row, extensionEntityKinds, issues);
2735
+ if (entity != null) try {
2736
+ entity = decodeEntityRow(assembleEntityContent(rows, row.entityId), extensionEntityKinds, issues);
2737
+ } catch (error) {
2738
+ issues.push(errorMessage(error));
2739
+ continue;
2740
+ }
2741
+ if (entity == null) continue;
2742
+ const ref = createEntityRef(entity);
2743
+ if (entitiesById.has(ref.entityId)) {
2744
+ issues.push(`duplicate entity id "${ref.entityId}"`);
2745
+ continue;
2746
+ }
2747
+ entitiesById.set(ref.entityId, ref);
2748
+ }
2749
+ const specsByKind = collectRelationSpecs(options.relationSpecs ?? [], issues);
2750
+ const relationIndex = new BiRelationIndex();
2751
+ const relations = [];
2752
+ const relationIds = /* @__PURE__ */ new Set();
2753
+ for (const row of rows.relations) {
2754
+ let relationId;
2755
+ try {
2756
+ relationId = createRelationId(row.relationId);
2757
+ } catch (error) {
2758
+ issues.push(errorMessage(error));
2759
+ continue;
2760
+ }
2761
+ if (relationIds.has(relationId)) {
2762
+ issues.push(`duplicate relation id "${relationId}"`);
2763
+ continue;
2764
+ }
2765
+ relationIds.add(relationId);
2766
+ if (!isTrimmedNonEmpty(row.relationKind)) {
2767
+ issues.push(`relation "${relationId}" has an empty or untrimmed kind`);
2768
+ continue;
2769
+ }
2770
+ const spec = specsByKind.get(row.relationKind);
2771
+ if (spec == null) {
2772
+ issues.push(`relation "${relationId}" has no registered spec for kind "${row.relationKind}"`);
2773
+ continue;
2774
+ }
2775
+ let endpoint0EntityId;
2776
+ let endpoint1EntityId;
2777
+ try {
2778
+ endpoint0EntityId = createEntityId(row.endpoint0EntityId);
2779
+ endpoint1EntityId = createEntityId(row.endpoint1EntityId);
2780
+ } catch (error) {
2781
+ issues.push(errorMessage(error));
2782
+ continue;
2783
+ }
2784
+ const endpoint0 = entitiesById.get(endpoint0EntityId);
2785
+ const endpoint1 = entitiesById.get(endpoint1EntityId);
2786
+ if (endpoint0 == null || endpoint1 == null) {
2787
+ const missing = [endpoint0 == null ? endpoint0EntityId : void 0, endpoint1 == null ? endpoint1EntityId : void 0].filter((value) => value != null).join(", ");
2788
+ issues.push(`relation "${relationId}" references missing entity id(s): ${missing}`);
2789
+ continue;
2790
+ }
2791
+ if (!isJsonObject(row.metadata) || !isJsonObject(row.trace)) {
2792
+ issues.push(`relation "${relationId}" metadata and trace must contain only JSON values`);
2793
+ continue;
2794
+ }
2795
+ try {
2796
+ relations.push(relationIndex.linkRuntime({
2797
+ relationId,
2798
+ spec,
2799
+ endpoints: [endpoint0, endpoint1],
2800
+ metadata: row.metadata,
2801
+ trace: row.trace
2802
+ }));
2803
+ } catch (error) {
2804
+ issues.push(errorMessage(error));
2805
+ }
2806
+ }
2807
+ if (issues.length === 0) for (const issue of validateEntityRelationSet([...entitiesById.values()], relationIndex, options)) issues.push(`${issue.code}: ${issue.message}`);
2808
+ if (issues.length > 0) throw new InvalidEntityRelationRowsError(issues);
2809
+ return {
2810
+ entitiesById,
2811
+ relationIndex,
2812
+ relations
2813
+ };
2814
+ }
2815
+ function decodeEntityRow(row, extensionEntityKinds, issues) {
2816
+ let entityId;
2817
+ try {
2818
+ entityId = createEntityId(row.entityId);
2819
+ } catch (error) {
2820
+ issues.push(errorMessage(error));
2821
+ return;
2822
+ }
2823
+ if (!isTrimmedNonEmpty(row.entityKind)) {
2824
+ issues.push(`entity "${entityId}" has an empty or untrimmed kind`);
2825
+ return;
2826
+ }
2827
+ if (isReservedEntityKind(row.entityKind)) {
2828
+ issues.push(`entity "${entityId}" uses reserved kind "${row.entityKind}"`);
2829
+ return;
2830
+ }
2831
+ if (!isKnownEntityKind(row.entityKind) && !extensionEntityKinds.has(row.entityKind)) {
2832
+ issues.push(`entity "${entityId}" has unregistered extension kind "${row.entityKind}"`);
2833
+ return;
2834
+ }
2835
+ if (!isJsonObject(row.payload)) {
2836
+ issues.push(`entity "${entityId}" payload must contain only JSON values`);
2837
+ return;
2838
+ }
2839
+ const reserved = ["entityId", "entityKind"].filter((key) => Object.hasOwn(row.payload, key));
2840
+ if (reserved.length > 0) {
2841
+ issues.push(`entity "${entityId}" payload contains reserved field(s): ${reserved.join(", ")}`);
2842
+ return;
2843
+ }
2844
+ return {
2845
+ ...row.payload,
2846
+ entityId,
2847
+ entityKind: row.entityKind
2848
+ };
2849
+ }
2850
+ function collectRelationSpecs(extensionSpecs, issues) {
2851
+ const specs = /* @__PURE__ */ new Map();
2852
+ for (const spec of builtInRelationSpecs) specs.set(spec.kind, spec);
2853
+ for (const spec of extensionSpecs) {
2854
+ if (!isTrimmedNonEmpty(spec.kind)) {
2855
+ issues.push("extension relation spec has an empty or untrimmed kind");
2856
+ continue;
2857
+ }
2858
+ if (specs.has(spec.kind)) {
2859
+ issues.push(`relation spec kind "${spec.kind}" is already registered`);
2860
+ continue;
2861
+ }
2862
+ specs.set(spec.kind, spec);
2863
+ }
2864
+ return specs;
2865
+ }
2866
+ function isTrimmedNonEmpty(value) {
2867
+ return value.length > 0 && value.trim() === value;
2868
+ }
2869
+ function errorMessage(error) {
2870
+ return error instanceof Error ? error.message : String(error);
2871
+ }
2872
+ //#endregion
2873
+ //#region src/document/entity-projection-values.ts
2874
+ /** A graph cannot be represented faithfully by the compatibility reader. */
2875
+ var EntityTimelineProjectionError = class extends Error {
2876
+ constructor(message) {
2877
+ super(message);
2878
+ this.name = "EntityTimelineProjectionError";
2879
+ }
2880
+ };
2881
+ function fail(message) {
2882
+ throw new EntityTimelineProjectionError(message);
2883
+ }
2884
+ function object(value) {
2885
+ return value !== void 0 && value !== null && typeof value === "object" && !Array.isArray(value);
2886
+ }
2887
+ function finite(value, label) {
2888
+ if (typeof value !== "number" || !Number.isFinite(value)) fail(`${label} must be finite`);
2889
+ return value;
2890
+ }
2891
+ function positive(value, label) {
2892
+ const result = finite(value, label);
2893
+ if (result <= 0) fail(`${label} must be positive`);
2894
+ return result;
2895
+ }
2896
+ function milliseconds(value, label) {
2897
+ const result = finite(value, label);
2898
+ if (!Number.isSafeInteger(result) || result < 0) fail(`${label} must be non-negative whole milliseconds`);
2899
+ return result;
2900
+ }
2901
+ function range(value, label) {
2902
+ if (!object(value)) fail(`${label} must be a numeric range`);
2903
+ const start = finite(value.start, `${label}.start`);
2904
+ const end = finite(value.end, `${label}.end`);
2905
+ if (start >= end) fail(`${label} must be non-empty`);
2906
+ return {
2907
+ start,
2908
+ end
2909
+ };
2910
+ }
2911
+ //#endregion
2912
+ //#region src/document/entity-timeline-layout.ts
2913
+ /** Validated graph lookup shared by the compatibility layout and projection. */
2914
+ var EntityProjectionGraph = class {
2915
+ rows;
2916
+ entities;
2917
+ timeline;
2918
+ constructor(rows) {
2919
+ this.rows = rows;
2920
+ const decoded = decodeEntityRelationRows(rows, { compareMarkerPoints: (_marker, _range, left, right) => finite(left, "range point") - finite(right, "range point") });
2921
+ this.entities = new Map([...decoded.entitiesById.values()].map((ref) => [String(ref.entityId), entityToRow(ref.current())]));
2922
+ const roots = [...this.entities.values()].filter((row) => row.entityKind === "timeline");
2923
+ if (roots.length !== 1) fail("compatibility projection requires exactly one Timeline");
2924
+ this.timeline = roots[0];
2925
+ }
2926
+ entity(id, kind) {
2927
+ const row = this.entities.get(id);
2928
+ if (row === void 0 || kind !== void 0 && row.entityKind !== kind) fail(`Expected ${kind ?? "Entity"} ${id}`);
2929
+ return row;
2930
+ }
2931
+ adjacent(id, kind) {
2932
+ return [...new Set(this.rows.relations.filter((row) => row.relationKind === kind && (row.endpoint0EntityId === id || row.endpoint1EntityId === id)).map((row) => row.endpoint0EntityId === id ? row.endpoint1EntityId : row.endpoint0EntityId))].map((entityId) => this.entity(entityId));
2933
+ }
2934
+ one(id, relation, kind) {
2935
+ const values = this.adjacent(id, relation);
2936
+ if (values.length !== 1 || kind !== void 0 && values[0]?.entityKind !== kind) fail(`entity ${id} requires exactly one ${relation}${kind === void 0 ? "" : ` to ${kind}`}`);
2937
+ return values[0];
2938
+ }
2939
+ };
2940
+ const roleOrder = {
2941
+ caption: 0,
2942
+ video_clip: 1,
2943
+ speech: 2,
2944
+ bgm: 3
2945
+ };
2946
+ /**
2947
+ * Resolve only entity-owned placement facts. This does not choose new anchors,
2948
+ * manufacture clips, or mutate a graph while reading. Write-side operations
2949
+ * must express any detach/reparent/overlap decision in their entity plan.
2950
+ */
2951
+ function resolveEntityTimelineLayout(rows) {
2952
+ return resolveGraphLayout(new EntityProjectionGraph(rows));
2953
+ }
2954
+ function resolveGraphLayout(graph) {
2955
+ const facts = /* @__PURE__ */ new Map();
2956
+ const seenRoles = /* @__PURE__ */ new Set();
2957
+ const tracks = graph.adjacent(graph.timeline.entityId, "timeline-track").map((track) => {
2958
+ const role = track.payload.role;
2959
+ if (track.entityKind !== "track" || typeof role !== "string" || !Object.hasOwn(roleOrder, role)) fail(`unsupported compatibility Track role on ${track.entityId}`);
2960
+ if (seenRoles.has(role)) fail(`The compatibility editor supports only one ${role} Track`);
2961
+ seenRoles.add(role);
2962
+ const typedRole = role;
2963
+ if (track.payload.hidden !== void 0 && typeof track.payload.hidden !== "boolean") fail("Track hidden must be boolean");
2964
+ const order = track.payload.order === void 0 ? roleOrder[typedRole] : finite(track.payload.order, "Track order");
2965
+ const trackFacts = graph.adjacent(track.entityId, "track-clip").map((clip) => {
2966
+ if (clip.entityKind !== "clip" || graph.adjacent(clip.entityId, "track-clip").length !== 1) fail(`Clip ${clip.entityId} must belong to exactly one Track`);
2967
+ const marker = graph.one(clip.entityId, "clip-marker", "sequence-marker");
2968
+ const content = graph.one(marker.entityId, "marker-content");
2969
+ if (!(typedRole === "video_clip" ? ["image", "video"] : typedRole === "speech" ? ["voice"] : typedRole === "caption" ? ["caption"] : ["audio"]).includes(content.entityKind)) fail(`unsupported compatibility content kind ${content.entityKind} on ${typedRole} Track`);
2970
+ const space = content.payload.coordinateSpace;
2971
+ if (space !== "ms" && space !== "milliseconds" && (!object(space) || space.unit !== "ms" && space.unit !== "milliseconds")) fail(`media ${content.entityId} must state millisecond coordinates for this reader`);
2972
+ const source = range(marker.payload.sourceRange, "sourceRange");
2973
+ const durationFact = marker.payload.duration;
2974
+ if (!object(durationFact)) fail(`Marker ${marker.entityId} requires duration`);
2975
+ const duration = positive(durationFact.mode === "from-source" ? source.end - source.start : durationFact.value, "duration");
2976
+ if (!Number.isSafeInteger(duration)) fail("Compatibility duration must be whole milliseconds");
2977
+ const fillTimeline = marker.payload.durationPolicy === "timeline";
2978
+ if (marker.payload.durationPolicy !== void 0 && (!fillTimeline || typedRole !== "bgm")) fail("durationPolicy is only supported for timeline-filling background Audio");
2979
+ const anchors = graph.rows.relations.filter((row) => row.relationKind === "clip-anchor" && row.endpoint0EntityId === clip.entityId);
2980
+ if (anchors.length > 1) fail(`Clip ${clip.entityId} has multiple anchors`);
2981
+ const target = marker.payload.targetRange === void 0 ? void 0 : range(marker.payload.targetRange, "targetRange");
2982
+ const anchorId = anchors[0]?.endpoint1EntityId;
2983
+ let mode;
2984
+ let absoluteStart;
2985
+ let anchorOffset;
2986
+ let clipOrder;
2987
+ if (anchorId !== void 0) {
2988
+ if (target !== void 0) fail(`Anchored Marker ${marker.entityId} cannot also have targetRange`);
2989
+ mode = "anchored";
2990
+ anchorOffset = milliseconds(marker.payload.anchorOffset, "anchorOffset");
2991
+ } else {
2992
+ if (marker.payload.anchorOffset !== void 0) fail(`Marker ${marker.entityId} has anchorOffset without a clip-anchor Relation`);
2993
+ if (target !== void 0) {
2994
+ mode = "absolute";
2995
+ absoluteStart = milliseconds(target.start, "target start");
2996
+ milliseconds(target.end, "target end");
2997
+ if (Math.abs(target.end - target.start - duration) > 1e-6) fail(`Marker ${marker.entityId} targetRange must match its effective duration`);
2998
+ } else {
2999
+ mode = "sequential";
3000
+ if (clip.payload.order === void 0) fail(`Clip ${clip.entityId} has no placement: targetRange or explicit order is required`);
3001
+ clipOrder = finite(clip.payload.order, "Clip order");
3002
+ }
3003
+ }
3004
+ const fact = {
3005
+ clip,
3006
+ marker,
3007
+ content,
3008
+ trackId: track.entityId,
3009
+ role: typedRole,
3010
+ duration,
3011
+ mode,
3012
+ order: clipOrder,
3013
+ absoluteStart,
3014
+ anchorId,
3015
+ anchorOffset,
3016
+ fillTimeline
3017
+ };
3018
+ facts.set(clip.entityId, fact);
3019
+ return fact;
3020
+ });
3021
+ if (typedRole === "bgm" && trackFacts.length > 1) fail("The compatibility editor supports only one background Audio Clip");
3022
+ const sequential = trackFacts.filter((fact) => fact.mode === "sequential").sort((left, right) => left.order - right.order);
3023
+ if (new Set(sequential.map((fact) => fact.order)).size !== sequential.length) fail("sequential Clips must have distinct order values");
3024
+ sequential.forEach((fact, index) => {
3025
+ fact.predecessor = sequential[index - 1]?.clip.entityId;
3026
+ });
3027
+ return {
3028
+ entityId: String(track.entityId),
3029
+ role: typedRole,
3030
+ order,
3031
+ hidden: track.payload.hidden === true,
3032
+ trackFacts
3033
+ };
3034
+ });
3035
+ tracks.sort((left, right) => left.order - right.order || left.entityId.localeCompare(right.entityId));
3036
+ const resolved = /* @__PURE__ */ new Map();
3037
+ const visiting = /* @__PURE__ */ new Set();
3038
+ const resolve = (id) => {
3039
+ const cached = resolved.get(id);
3040
+ if (cached !== void 0) return cached;
3041
+ if (visiting.has(id)) fail(`Cyclic Clip placement dependency at ${id}`);
3042
+ visiting.add(id);
3043
+ const fact = facts.get(id);
3044
+ if (fact === void 0) fail(`Placement refers to unplaced Clip ${id}`);
3045
+ let start = fact.absoluteStart ?? 0;
3046
+ if (fact.mode === "anchored") {
3047
+ const host = resolve(fact.anchorId);
3048
+ if (fact.role === "speech" && !["video_clip", "bgm"].includes(facts.get(host.clipEntityId).role)) fail("An anchored Voice must use a visual or BGM Clip as its host");
3049
+ start = host.start + fact.anchorOffset;
3050
+ } else if (fact.predecessor !== void 0) {
3051
+ const prior = resolve(fact.predecessor);
3052
+ start = prior.start + prior.duration;
3053
+ }
3054
+ milliseconds(start + fact.duration, "Clip end");
3055
+ const result = {
3056
+ clipEntityId: id,
3057
+ markerEntityId: fact.marker.entityId,
3058
+ contentEntityId: fact.content.entityId,
3059
+ trackEntityId: fact.trackId,
3060
+ start: milliseconds(start, "Clip start"),
3061
+ duration: fact.duration,
3062
+ mode: fact.mode,
3063
+ ...fact.anchorId === void 0 ? {} : {
3064
+ anchorClipEntityId: fact.anchorId,
3065
+ anchorOffset: fact.anchorOffset
3066
+ }
3067
+ };
3068
+ visiting.delete(id);
3069
+ resolved.set(id, result);
3070
+ return result;
3071
+ };
3072
+ for (const id of facts.keys()) resolve(id);
3073
+ const durationMs = Math.max(0, ...[...resolved.values()].filter((clip) => facts.get(clip.clipEntityId).role !== "bgm").map((clip) => clip.start + clip.duration));
3074
+ for (const [id, fact] of facts) if (fact.fillTimeline) {
3075
+ const clip = resolved.get(id);
3076
+ resolved.set(id, {
3077
+ ...clip,
3078
+ duration: durationMs
3079
+ });
3080
+ }
3081
+ return {
3082
+ durationMs,
3083
+ clips: resolved,
3084
+ tracks: tracks.map(({ trackFacts, ...track }) => ({
3085
+ ...track,
3086
+ clips: trackFacts.map((fact) => resolved.get(fact.clip.entityId)).sort((left, right) => left.start - right.start || left.clipEntityId.localeCompare(right.clipEntityId))
3087
+ }))
3088
+ };
3089
+ }
3090
+ //#endregion
3091
+ //#region src/document/entity-projection.ts
3092
+ /**
3093
+ * One-way compatibility view over the Entity graph. A legacy part is never
3094
+ * consulted as an editing fact. Peer IDs appear only in this derived read
3095
+ * contract, reconstructed from relations; they are not stored in payloads.
3096
+ */
3097
+ function projectEntityTimeline(rows, meta) {
3098
+ const graph = new EntityProjectionGraph(rows);
3099
+ const layout = resolveGraphLayout(graph);
3100
+ for (const track of layout.tracks) {
3101
+ if (track.role !== "video_clip") continue;
3102
+ for (let index = 1; index < track.clips.length; index += 1) {
3103
+ const previous = track.clips[index - 1];
3104
+ if (track.clips[index].start < previous.start + previous.duration) fail("Overlapping visual Clips require an explicit overlay adapter; the current editor supports one visual lane");
3105
+ }
3106
+ }
3107
+ const parts = {};
3108
+ for (const track of layout.tracks) for (const placement of track.clips) {
3109
+ const clip = graph.entity(placement.clipEntityId, "clip");
3110
+ const marker = graph.entity(placement.markerEntityId, "sequence-marker");
3111
+ const content = graph.entity(placement.contentEntityId);
3112
+ const volume = volumeSchema.safeParse(clip.payload.volume ?? 0);
3113
+ if (!volume.success) fail("Clip volume must be decibels between -60 and 20");
3114
+ if (track.role === "video_clip") {
3115
+ const asset = mediaAssetFacts(graph, content);
3116
+ const source = range(marker.payload.sourceRange, "sourceRange");
3117
+ const external = externalLocator(asset, "memota");
3118
+ const speed = videoSpeed(content, marker, placement);
3119
+ parts[clip.entityId] = { video_clip: {
3120
+ id: clip.entityId,
3121
+ kind: "video_clip",
3122
+ origin_media_id: external.key,
3123
+ play_in: content.entityKind === "image" && speed === void 0 ? 0 : source.start,
3124
+ play_out: content.entityKind === "image" && speed === void 0 ? placement.duration : source.end,
3125
+ volume: volume.data,
3126
+ ...speed === void 0 ? {} : { speed_shift: speed }
3127
+ } };
3128
+ } else if (track.role === "speech") {
3129
+ const asset = mediaAssetFacts(graph, content);
3130
+ requireUntrimmedAudio(content, marker, placement);
3131
+ const external = externalLocator(asset, "memota-speech");
3132
+ const assembled = graph.adjacent(content.entityId, "phonetic-script-render").length > 0 || content.payload.voice !== void 0 ? assemblePhoneticScriptContent(rows, graph.one(content.entityId, "phonetic-script-render", "phonetic-script").entityId) : void 0;
3133
+ const voice = content.payload.voice;
3134
+ if (voice !== void 0 && (!object(voice) || voice.system !== "voice-library" || typeof voice.key !== "string")) fail("Voice must use an explicit voice-library locator");
3135
+ const captionIds = layout.tracks.flatMap((candidate) => candidate.role === "caption" ? candidate.clips.filter((caption) => caption.anchorClipEntityId === clip.entityId).map((caption) => caption.clipEntityId) : []);
3136
+ parts[clip.entityId] = { speech: {
3137
+ id: clip.entityId,
3138
+ kind: "speech",
3139
+ media_duration_ms: placement.duration,
3140
+ audio_storage_key: storageKey(asset),
3141
+ origin_speech_id: external.key,
3142
+ audio_script: assembled?.text ?? (graph.adjacent(content.entityId, "audio-script-source").length > 0 ? assembleScriptText(graph.one(content.entityId, "audio-script-source", "audio-script")) : ""),
3143
+ volume: volume.data,
3144
+ voice: object(voice) ? {
3145
+ id: typeof voice.key === "string" ? voice.key : void 0,
3146
+ name: typeof voice.name === "string" ? voice.name : void 0
3147
+ } : void 0,
3148
+ caption_ids: captionIds
3149
+ } };
3150
+ } else if (track.role === "caption") {
3151
+ const assembled = assembleCaptionContent(rows, content.entityId);
3152
+ const text = assembled.text;
3153
+ const cues = captionDisplayCues(assembled, marker, placement.duration);
3154
+ if (marker.payload.timeRemapping !== void 0) fail("Caption timeRemapping is not supported by this reader");
3155
+ const style = captionStyle(content.payload.style);
3156
+ const anchor = placement.anchorClipEntityId === void 0 ? void 0 : layout.clips.get(placement.anchorClipEntityId);
3157
+ const anchoredToSpeech = anchor !== void 0 && trackRoleOf(layout, anchor.trackEntityId) === "speech";
3158
+ parts[clip.entityId] = { caption: {
3159
+ id: clip.entityId,
3160
+ kind: "caption",
3161
+ text,
3162
+ ...cues === void 0 ? {} : { display_cues: cues },
3163
+ speech_part_id: anchoredToSpeech ? placement.anchorClipEntityId : void 0,
3164
+ start_ms: anchoredToSpeech ? placement.anchorOffset : 0,
3165
+ initial_duration_ms: placement.duration,
3166
+ ...style === void 0 ? {} : { style }
3167
+ } };
3168
+ } else {
3169
+ const asset = mediaAssetFacts(graph, content);
3170
+ const external = externalLocator(asset, "memota");
3171
+ if (marker.payload.durationPolicy !== "timeline") fail("Background Audio requires an explicit timeline-fill durationPolicy");
3172
+ if (marker.payload.timeRemapping !== void 0) fail("BGM timeRemapping is not supported by this reader");
3173
+ const source = range(marker.payload.sourceRange, "sourceRange");
3174
+ const extent = content.payload.extent;
3175
+ if (!object(extent) || extent.start !== source.start || extent.end !== source.end) fail("Background Audio compatibility requires the complete source");
3176
+ parts[clip.entityId] = { bgm: {
3177
+ id: clip.entityId,
3178
+ kind: "bgm",
3179
+ audio_storage_key: storageKey(asset),
3180
+ origin_media_id: external.key,
3181
+ volume: volume.data
3182
+ } };
3183
+ }
3184
+ }
3185
+ const document = {
3186
+ meta: {
3187
+ ...structuredClone(meta),
3188
+ schema_version: ENTITY_TIMELINE_PROJECTION_SCHEMA_VERSION
3189
+ },
3190
+ timeline: graph.timeline.payload.unit_time_ms === void 0 ? void 0 : { unit_time_ms: positive(graph.timeline.payload.unit_time_ms, "unit_time_ms") },
3191
+ tracks: layout.tracks.map((track) => ({
3192
+ id: track.entityId,
3193
+ parts_kind: track.role,
3194
+ is_hidden: track.hidden,
3195
+ items: track.clips.map((clip) => clip.mode === "anchored" ? {
3196
+ part_id: clip.clipEntityId,
3197
+ time_position: {
3198
+ mode: "anchored",
3199
+ anchorPartId: clip.anchorClipEntityId,
3200
+ offsetMs: clip.anchorOffset
3201
+ },
3202
+ fallback_abs_ms: clip.start
3203
+ } : {
3204
+ part_id: clip.clipEntityId,
3205
+ time_position: {
3206
+ mode: "absolute",
3207
+ offsetMs: clip.start
3208
+ }
3209
+ })
3210
+ })),
3211
+ part_library: parts
3212
+ };
3213
+ assertValidVideoDocument(document);
3214
+ return document;
3215
+ }
3216
+ function trackRoleOf(layout, trackEntityId) {
3217
+ return layout.tracks.find((track) => track.entityId === trackEntityId)?.role;
3218
+ }
3219
+ /**
3220
+ * Media facts are read from the variant itself: a media variant owns its
3221
+ * Asset identity and carries `external`/`storageKey` directly. Dual-entity
3222
+ * media graphs are rejected instead of being resolved through a legacy
3223
+ * `physical-asset` binding.
3224
+ */
3225
+ function mediaAssetFacts(graph, content) {
3226
+ if (graph.adjacent(content.entityId, "physical-asset").length > 0) fail("Media " + content.entityId + " owns its Asset identity; remove its physical-asset Relation");
3227
+ if (!object(content.payload.external)) fail("Media " + content.entityId + " must carry its own Asset external identity");
3228
+ return {
3229
+ entityId: String(content.entityId),
3230
+ payload: content.payload
3231
+ };
3232
+ }
3233
+ function externalLocator(asset, system) {
3234
+ const value = asset.payload.external;
3235
+ if (!object(value) || value.system !== system || typeof value.key !== "string" || value.key.trim() === "") fail("Asset " + asset.entityId + " requires an explicit " + system + " external key");
3236
+ return { key: value.key };
3237
+ }
3238
+ function storageKey(asset) {
3239
+ if (typeof asset.payload.storageKey !== "string" || asset.payload.storageKey.trim() === "") fail("Asset " + asset.entityId + " requires its factual storageKey");
3240
+ return asset.payload.storageKey;
3241
+ }
3242
+ function captionStyle(value) {
3243
+ if (value === void 0) return void 0;
3244
+ if (!object(value)) fail("Caption style must be an object");
3245
+ const { font, ...owned } = structuredClone(value);
3246
+ const fields = {
3247
+ fontSize: "font_size",
3248
+ fontColor: "font_color",
3249
+ fontWeight: "font_weight",
3250
+ entranceAnimation: "entrance_animation",
3251
+ entranceAnimationDurationMs: "entrance_animation_duration_ms",
3252
+ strokeColor: "stroke_color",
3253
+ strokeWidth: "stroke_width",
3254
+ positionX: "position_x",
3255
+ positionY: "position_y"
3256
+ };
3257
+ const style = {};
3258
+ for (const [key, field] of Object.entries(owned)) {
3259
+ if (!Object.hasOwn(fields, key)) fail("Unsupported Caption style field " + key);
3260
+ style[fields[key]] = field;
3261
+ }
3262
+ if (font !== void 0) {
3263
+ if (!object(font) || font.system !== "font-library" || typeof font.key !== "string") fail("Caption font requires a font-library locator");
3264
+ return {
3265
+ ...style,
3266
+ font_id: font.key
3267
+ };
3268
+ }
3269
+ return style;
3270
+ }
3271
+ function requireUntrimmedAudio(content, marker, placement) {
3272
+ const source = range(marker.payload.sourceRange, "sourceRange");
3273
+ const extent = content.payload.extent;
3274
+ if (!object(extent) || extent.start !== source.start || extent.end !== source.end || placement.duration !== source.end - source.start || marker.payload.timeRemapping !== void 0) fail("Voice compatibility requires the complete, unremapped take");
3275
+ }
3276
+ function videoSpeed(content, marker, placement) {
3277
+ const remapping = marker.payload.timeRemapping;
3278
+ if (content.entityKind === "image" && remapping === void 0) return void 0;
3279
+ const source = range(marker.payload.sourceRange, "sourceRange");
3280
+ if (remapping !== void 0) {
3281
+ if (!object(remapping) || remapping.kind !== "linear") fail("unsupported timeRemapping on " + marker.entityId);
3282
+ if (Object.keys(remapping).some((key) => key !== "kind" && key !== "rate" && key !== "mode")) fail("Unsupported linear timeRemapping fields");
3283
+ const rate = positive(remapping.rate, "linear remapping rate");
3284
+ if (Math.round((source.end - source.start) / rate) !== placement.duration) fail("timeRemapping on " + marker.entityId + " must agree with Marker duration");
3285
+ if (remapping.mode !== void 0 && typeof remapping.mode !== "string") fail("Linear remapping mode must be a string");
3286
+ return {
3287
+ category: "linear",
3288
+ mode: typeof remapping.mode === "string" ? remapping.mode : "constant",
3289
+ config: { linear: { speed: rate } }
3290
+ };
3291
+ }
3292
+ const rate = (source.end - source.start) / placement.duration;
3293
+ return rate === 1 ? void 0 : {
3294
+ category: "linear",
3295
+ mode: "constant",
3296
+ config: { linear: { speed: rate } }
3297
+ };
3298
+ }
3299
+ /** Preserve selected text intervals without minting synthetic editable Clip identities. */
3300
+ function captionDisplayCues(assembled, marker, duration) {
3301
+ const ranges = assembled.caption.payload.segmentRanges;
3302
+ if (!Array.isArray(ranges)) return void 0;
3303
+ const source = range(marker.payload.sourceRange, "Caption sourceRange");
3304
+ const scale = duration / (source.end - source.start);
3305
+ return assembled.segments.flatMap((segment) => {
3306
+ const timing = ranges.find((entry) => object(entry) && entry.segmentId === segment.segmentId);
3307
+ if (!object(timing)) fail(`Caption timing is missing for selected segment ${segment.segmentId}`);
3308
+ const intrinsic = range({
3309
+ start: timing.startMs,
3310
+ end: timing.endMs
3311
+ }, "Caption segmentRange");
3312
+ const start = Math.max(source.start, intrinsic.start);
3313
+ const end = Math.min(source.end, intrinsic.end);
3314
+ if (end <= start) return [];
3315
+ const startMs = Math.round((start - source.start) * scale);
3316
+ const endMs = Math.round((end - source.start) * scale);
3317
+ return endMs > startMs ? [{
3318
+ segmentId: segment.segmentId,
3319
+ text: segment.text,
3320
+ startMs,
3321
+ durationMs: endMs - startMs
3322
+ }] : [];
3323
+ });
3324
+ }
3325
+ //#endregion
3326
+ //#region src/entity-crdt/field-changes.ts
3327
+ const atomicFields = new Set([
3328
+ "sequence",
3329
+ "sourceRange",
3330
+ "targetRange",
3331
+ "duration",
3332
+ "extent",
3333
+ "timeRemapping",
3334
+ "textRange",
3335
+ "selection",
3336
+ "external",
3337
+ "coordinateSpace"
3338
+ ]);
3339
+ const textFields = new Set(["text", "phonemeScript"]);
3340
+ const orderedFields = new Set([
3341
+ "segments",
3342
+ "segmentRanges",
3343
+ "baseEntityIds"
3344
+ ]);
3345
+ const unsafeKeys = new Set([
3346
+ "__proto__",
3347
+ "prototype",
3348
+ "constructor"
3349
+ ]);
3350
+ function listElementId(value) {
3351
+ const id = typeof value === "string" ? value : isJsonObject(value) ? value.segmentId : void 0;
3352
+ if (typeof id !== "string" || !id.trim()) throw new Error("List elements require a nonempty segmentId or string identity");
3353
+ return id;
3354
+ }
3355
+ function assertFieldChanges(changes) {
3356
+ if (!Array.isArray(changes) || changes.length === 0) throw new Error("changes must be a nonempty array");
3357
+ for (const change of changes) {
3358
+ if (!isJsonObject(change) || ![
3359
+ "set",
3360
+ "unset",
3361
+ "text.splice",
3362
+ "list.insert",
3363
+ "list.move",
3364
+ "list.remove"
3365
+ ].includes(change.op)) throw new Error("Unknown field operation");
3366
+ if (!Array.isArray(change.path) || !change.path.length || typeof change.path[0] !== "string") throw new Error("Field paths must start with a field name");
3367
+ for (const part of change.path) if (typeof part === "string") {
3368
+ if (!part || unsafeKeys.has(part)) throw new Error("Invalid field name");
3369
+ } else if (!isJsonObject(part) || Object.keys(part).length !== 1 || typeof part.elementId !== "string" || !part.elementId.trim()) throw new Error("List paths require { elementId }; numeric indices are not supported");
3370
+ }
3371
+ }
3372
+ /** Resolve against the sequential causal view; the numeric path never crosses the API boundary. */
3373
+ function resolveFieldPath(payload, path) {
3374
+ let value = payload;
3375
+ const resolved = [];
3376
+ for (const [index, part] of path.entries()) if (typeof part === "string") {
3377
+ if (!isJsonObject(value)) throw new Error(`Field path does not reach a map: ${part}`);
3378
+ if (index < path.length - 1 && atomicFields.has(part)) throw new Error(`Atomic field ${part} must be changed as one value`);
3379
+ resolved.push(part);
3380
+ value = value[part];
3381
+ } else {
3382
+ if (!Array.isArray(value)) throw new Error("Element selector requires a list");
3383
+ const matches = value.flatMap((item, i) => listElementId(item) === part.elementId ? [i] : []);
3384
+ if (matches.length !== 1) throw new Error(`Missing or ambiguous list element ${part.elementId}`);
3385
+ resolved.push(matches[0]);
3386
+ value = value[matches[0]];
3387
+ }
3388
+ return resolved;
3389
+ }
3390
+ /** Detached plan evaluation. Native compilation replays the original operations, not this snapshot. */
3391
+ function applyFieldChanges(payload, changes) {
3392
+ assertFieldChanges(changes);
3393
+ const result = structuredClone(payload);
3394
+ for (const change of changes) {
3395
+ const path = resolveFieldPath(result, change.path);
3396
+ const key = path.at(-1);
3397
+ if (typeof key !== "string") throw new Error("Change fields inside a list element or use list operations");
3398
+ let parent = result;
3399
+ for (const part of path.slice(0, -1)) parent = parent[part];
3400
+ if (!isJsonObject(parent)) throw new Error("Field parent is not a map");
3401
+ const mutable = parent;
3402
+ const value = mutable[key];
3403
+ if ([
3404
+ "entityId",
3405
+ "entityKind",
3406
+ "entity_id",
3407
+ "entity_kind",
3408
+ "segmentId"
3409
+ ].includes(key)) throw new Error(`Identity field ${key} cannot be edited`);
3410
+ switch (change.op) {
3411
+ case "set":
3412
+ if (textFields.has(key) || orderedFields.has(key) || !atomicFields.has(key) && (isJsonObject(value) || isJsonObject(change.value) || Array.isArray(value) || Array.isArray(change.value))) throw new Error(`Field ${key} requires a text, list, or leaf operation; snapshot replacement is not allowed`);
3413
+ if (change.value === void 0 || !isJsonObject({ value: change.value })) throw new Error("set requires a JSON value");
3414
+ if (value !== void 0 && value !== null && change.value !== null && typeof value !== typeof change.value) throw new Error(`Field ${key} cannot change type`);
3415
+ mutable[key] = structuredClone(change.value);
3416
+ break;
3417
+ case "unset":
3418
+ if (!Object.hasOwn(parent, key)) throw new Error(`Field ${key} is absent`);
3419
+ delete mutable[key];
3420
+ break;
3421
+ case "text.splice": {
3422
+ if (!textFields.has(key) || typeof value !== "string") throw new Error(`Field ${key} is not collaborative text`);
3423
+ const chars = Array.from(value);
3424
+ if (!Number.isSafeInteger(change.index) || !Number.isSafeInteger(change.deleteCount) || change.index < 0 || change.deleteCount < 0 || change.index + change.deleteCount > chars.length || typeof change.text !== "string") throw new Error("Invalid text splice range");
3425
+ chars.splice(change.index, change.deleteCount, ...Array.from(change.text));
3426
+ mutable[key] = chars.join("");
3427
+ break;
3428
+ }
3429
+ default: {
3430
+ if (!orderedFields.has(key) || !Array.isArray(value)) throw new Error(`Field ${key} is not a collaborative list`);
3431
+ const ids = value.map(listElementId);
3432
+ if (new Set(ids).size !== ids.length) throw new Error("Duplicate collaborative list identity");
3433
+ const requireIndex = (id) => {
3434
+ const index = ids.indexOf(id);
3435
+ if (index < 0) throw new Error(`Unknown list element ${id}`);
3436
+ return index;
3437
+ };
3438
+ if (change.op === "list.remove") value.splice(requireIndex(change.elementId), 1);
3439
+ else {
3440
+ if (change.beforeElementId !== null && typeof change.beforeElementId !== "string") throw new Error("beforeElementId must be an identity or null");
3441
+ const before = change.beforeElementId === null ? value.length : requireIndex(change.beforeElementId);
3442
+ if (change.op === "list.insert") {
3443
+ const id = listElementId(change.value);
3444
+ if (ids.includes(id)) throw new Error(`Duplicate list element ${id}`);
3445
+ value.splice(before, 0, structuredClone(change.value));
3446
+ } else {
3447
+ if (change.elementId === change.beforeElementId) throw new Error("Cannot move a list element before itself");
3448
+ const from = requireIndex(change.elementId);
3449
+ const [item] = value.splice(from, 1);
3450
+ value.splice(before > from ? before - 1 : before, 0, item);
3451
+ }
3452
+ }
3453
+ }
3454
+ }
3455
+ }
3456
+ return result;
3457
+ }
3458
+ //#endregion
3459
+ //#region src/entity-crdt/json-containers.ts
3460
+ function equalJson(a, b) {
3461
+ return canonicalJson(a) === canonicalJson(b);
3462
+ }
3463
+ function canonicalJson(value) {
3464
+ if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
3465
+ if (value !== null && typeof value === "object") return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`).join(",")}}`;
3466
+ return JSON.stringify(value);
3467
+ }
3468
+ /** Compile only the changed fields; untouched containers retain their operation identity. */
3469
+ function writeObject(map, before, after) {
3470
+ for (const key of Object.keys(before)) if (!Object.hasOwn(after, key)) map.delete(key);
3471
+ for (const [key, value] of Object.entries(after)) {
3472
+ if (equalJson(before[key], value)) continue;
3473
+ if (atomicFields.has(key)) map.set(key, value);
3474
+ else if (typeof value === "string" && textFields.has(key)) map.ensureMergeableText(key).update(value);
3475
+ else if (Array.isArray(value) && orderedFields.has(key)) writeList(map.ensureMergeableMovableList(key), value);
3476
+ else if (isJsonObject(value)) writeObject(map.ensureMergeableMap(key), isJsonObject(before[key]) ? before[key] : {}, value);
3477
+ else map.set(key, value);
3478
+ }
3479
+ }
3480
+ function readItem(list, index) {
3481
+ const value = list.get(index);
3482
+ return value instanceof LoroMap ? value.toJSON() : value;
3483
+ }
3484
+ function writeList(list, values) {
3485
+ const wanted = values.map(listElementId);
3486
+ if (new Set(wanted).size !== wanted.length) throw new Error("Duplicate collaborative list identity");
3487
+ for (let index = list.length - 1; index >= 0; index--) if (!wanted.includes(listElementId(readItem(list, index)))) list.delete(index, 1);
3488
+ for (let index = 0; index < values.length; index++) {
3489
+ const value = values[index];
3490
+ const key = wanted[index];
3491
+ let found = -1;
3492
+ for (let cursor = index; cursor < list.length; cursor++) if (listElementId(readItem(list, cursor)) === key) {
3493
+ found = cursor;
3494
+ break;
3495
+ }
3496
+ if (found === -1) if (isJsonObject(value)) writeObject(list.insertContainer(index, new LoroMap()), {}, value);
3497
+ else list.insert(index, value);
3498
+ else {
3499
+ if (found !== index) list.move(found, index);
3500
+ const current = list.get(index);
3501
+ if (isJsonObject(value)) {
3502
+ if (!(current instanceof LoroMap)) throw new Error("Collaborative list item changed type");
3503
+ writeObject(current, current.toJSON(), value);
3504
+ }
3505
+ }
3506
+ }
3507
+ }
3508
+ function requireText(map, path) {
3509
+ let value = map;
3510
+ for (const part of path) if (value instanceof LoroMap && typeof part === "string") value = value.get(part);
3511
+ else if (value instanceof LoroMovableList && typeof part === "number") value = value.get(part);
3512
+ else throw new Error("Invalid collaborative text path");
3513
+ if (!(value instanceof LoroText)) throw new Error("Field is not collaborative text");
3514
+ return value;
3515
+ }
3516
+ /** Apply the authored operation directly, preserving text positions and moved item identity. */
3517
+ function applyNativeFieldChange(map, path, change) {
3518
+ let parent = map;
3519
+ for (const part of path.slice(0, -1)) if (parent instanceof LoroMap && typeof part === "string") parent = parent.get(part);
3520
+ else if (parent instanceof LoroMovableList && typeof part === "number") parent = parent.get(part);
3521
+ else throw new Error("Invalid native field path");
3522
+ const key = path.at(-1);
3523
+ if (!(parent instanceof LoroMap) || typeof key !== "string") throw new Error("Invalid native field parent");
3524
+ if (change.op === "set") {
3525
+ parent.set(key, change.value);
3526
+ return;
3527
+ }
3528
+ if (change.op === "unset") {
3529
+ parent.delete(key);
3530
+ return;
3531
+ }
3532
+ const value = parent.get(key);
3533
+ if (change.op === "text.splice") {
3534
+ if (!(value instanceof LoroText)) throw new Error("Field is not collaborative text");
3535
+ const start = value.convertPos(change.index, "unicode", "utf16");
3536
+ const end = value.convertPos(change.index + change.deleteCount, "unicode", "utf16");
3537
+ value.splice(start, end - start, change.text);
3538
+ return;
3539
+ }
3540
+ if (!(value instanceof LoroMovableList)) throw new Error("Field is not a collaborative list");
3541
+ applyNativeListChange(value, change);
3542
+ }
3543
+ function applyNativeListChange(list, change) {
3544
+ const ids = Array.from({ length: list.length }, (_, i) => listElementId(readItem(list, i)));
3545
+ const index = (id) => {
3546
+ const found = ids.indexOf(id);
3547
+ if (found < 0) throw new Error(`Unknown list element ${id}`);
3548
+ return found;
3549
+ };
3550
+ if (change.op === "list.remove") {
3551
+ list.delete(index(change.elementId), 1);
3552
+ return;
3553
+ }
3554
+ const before = change.beforeElementId === null ? list.length : index(change.beforeElementId);
3555
+ if (change.op === "list.insert") if (isJsonObject(change.value)) writeObject(list.insertContainer(before, new LoroMap()), {}, change.value);
3556
+ else list.insert(before, change.value);
3557
+ else {
3558
+ const from = index(change.elementId);
3559
+ const to = before > from ? before - 1 : before;
3560
+ if (from !== to) list.move(from, to);
3561
+ }
3562
+ }
3563
+ //#endregion
3564
+ //#region src/entity-crdt/audio-script-assets.ts
3565
+ /** A resource is a snapshot of text; the native Loro text remains the editing authority. */
3566
+ function audioScriptAssetContent(payload) {
3567
+ assembleScriptText({
3568
+ entityId: createEntityId("script-validation"),
3569
+ entityKind: "audio-script",
3570
+ payload
3571
+ });
3572
+ return { segments: structuredClone(payload.segments) };
3573
+ }
3574
+ function audioScriptAssetHash(payload) {
3575
+ return bytesToHex(sha256(new TextEncoder().encode(canonicalJson(audioScriptAssetContent(payload)))));
3576
+ }
3577
+ function hasAudioScriptAsset(payload) {
3578
+ return isJsonObject(payload.external) && payload.external.system === "memota" && typeof payload.external.key === "string";
3579
+ }
3580
+ /** Internal fingerprint prevents a resource from silently describing an older text revision. */
3581
+ function audioScriptAssetFields(payload, assetId) {
3582
+ if (!assetId.trim()) throw new Error("AudioScript resource writer returned an empty asset ID");
3583
+ return {
3584
+ external: {
3585
+ system: "memota",
3586
+ key: assetId
3587
+ },
3588
+ assetContentHash: audioScriptAssetHash(payload)
3589
+ };
3590
+ }
3591
+ //#endregion
3592
+ //#region src/entity-crdt/ordering.ts
3593
+ /** Domain order is scoped by membership; relation storage has no global order. */
3594
+ function entityOrderGroups(rows) {
3595
+ const entities = new Map(rows.entities.map((row) => [String(row.entityId), row]));
3596
+ const groups = /* @__PURE__ */ new Map();
3597
+ for (const relation of rows.relations) {
3598
+ const childKind = relation.relationKind === "timeline-track" ? "track" : relation.relationKind === "track-clip" ? "clip" : null;
3599
+ if (!childKind) continue;
3600
+ const endpoints = [relation.endpoint0EntityId, relation.endpoint1EntityId];
3601
+ const childId = endpoints.find((id) => entities.get(id)?.entityKind === childKind);
3602
+ if (!childId) continue;
3603
+ const child = entities.get(childId);
3604
+ if (typeof child.payload.order !== "number") continue;
3605
+ const parent = endpoints.find((id) => id !== childId);
3606
+ const values = groups.get(parent) ?? [];
3607
+ values.push({
3608
+ id: childId,
3609
+ order: child.payload.order
3610
+ });
3611
+ groups.set(parent, values);
3612
+ }
3613
+ return new Map([...groups].map(([parent, values]) => [parent, values.sort((a, b) => a.order - b.order || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)).map((item) => item.id)]));
3614
+ }
3615
+ function ordering(doc) {
3616
+ const map = doc.getMap("medeo").get("ordering");
3617
+ if (!(map instanceof LoroMap)) throw new Error("Missing native entity ordering");
3618
+ return map;
3619
+ }
3620
+ /** Returns a derived ordinal while the stable element retains its native move identity. */
3621
+ function entityOrdinal(doc, root) {
3622
+ let found;
3623
+ for (const key of ordering(doc).keys()) {
3624
+ const list = ordering(doc).get(key);
3625
+ if (!(list instanceof LoroMovableList)) throw new Error("Invalid entity order container");
3626
+ const index = list.toArray().indexOf(root);
3627
+ if (index < 0) continue;
3628
+ if (found !== void 0) throw new Error("Entity belongs to multiple ordered groups");
3629
+ found = index;
3630
+ }
3631
+ return found;
3632
+ }
3633
+ /** Move existing elements, insert new identities and delete removed members on the causal list. */
3634
+ function writeEntityOrder(doc, group, roots, token) {
3635
+ if (new Set(roots).size !== roots.length) throw new Error("Duplicate ordered entity identity");
3636
+ const list = ordering(doc).ensureMergeableMovableList(group);
3637
+ const before = list.toArray();
3638
+ for (let index = list.length - 1; index >= 0; index--) if (!roots.includes(String(list.get(index)))) list.delete(index, 1);
3639
+ roots.forEach((root, index) => {
3640
+ const current = list.toArray().indexOf(root);
3641
+ if (current < 0) list.insert(index, root);
3642
+ else if (current !== index) list.move(current, index);
3643
+ });
3644
+ if (token) {
3645
+ const editing = doc.getMap("medeo").get("editing");
3646
+ for (const root of new Set([...before, ...roots])) {
3647
+ if (before.indexOf(root) < 0 || before.indexOf(root) === roots.indexOf(root)) continue;
3648
+ editing.get(root).get("changes").set(token, true);
3649
+ }
3650
+ }
3651
+ }
3652
+ //#endregion
3653
+ //#region src/entity-crdt/entity-document.ts
3654
+ const LORO_ENTITY_SCHEMA = "medeo.entities.loro.v1";
3655
+ const MATERIALIZE_ORIGIN = "medeo.materialize";
3656
+ function hash(value) {
3657
+ return bytesToHex(sha256(new TextEncoder().encode(canonicalJson(value))));
3658
+ }
3659
+ function clone(value) {
3660
+ return structuredClone(value);
3661
+ }
3662
+ function root(doc) {
3663
+ return doc.getMap("medeo");
3664
+ }
3665
+ function requireMap(parent, key) {
3666
+ const value = parent.get(key);
3667
+ if (!(value instanceof LoroMap)) throw new Error(`Missing Loro map ${key}`);
3668
+ return value;
3669
+ }
3670
+ function section(doc, key) {
3671
+ return requireMap(root(doc), key);
3672
+ }
3673
+ function workspace(doc, id) {
3674
+ const value = section(doc, "editing").get(id);
3675
+ if (!(value instanceof LoroMap)) throw new Error(`Unknown entity editing identity ${id}`);
3676
+ return value;
3677
+ }
3678
+ function content(map) {
3679
+ return requireMap(map, "content").toJSON();
3680
+ }
3681
+ function entityContent(doc, key) {
3682
+ const payload = content(workspace(doc, key));
3683
+ const order = entityOrdinal(doc, key);
3684
+ return order === void 0 ? payload : {
3685
+ ...payload,
3686
+ order
3687
+ };
3688
+ }
3689
+ function bases(map) {
3690
+ const links = requireMap(map, "bases");
3691
+ return links.keys().sort((a, b) => a < b ? -1 : a > b ? 1 : 0).map((key) => String(links.get(key)));
3692
+ }
3693
+ function owned(payload) {
3694
+ const { baseEntityIds: _bases, ...fields } = payload;
3695
+ return fields;
3696
+ }
3697
+ function baseIds(payload) {
3698
+ const ids = payload.baseEntityIds ?? [];
3699
+ if (!Array.isArray(ids) || !ids.every((id) => typeof id === "string") || new Set(ids).size !== ids.length) throw new Error("baseEntityIds must contain unique entity IDs");
3700
+ return ids;
3701
+ }
3702
+ function family(doc, id) {
3703
+ const value = section(doc, "origins").get(id);
3704
+ if (typeof value === "string") return value;
3705
+ for (const [key, value] of currentIds(doc)) if (value === id) return key;
3706
+ throw new Error(`Unknown entity ${id}`);
3707
+ }
3708
+ function currentIds(doc) {
3709
+ const result = /* @__PURE__ */ new Map();
3710
+ for (const key of section(doc, "editing").keys().sort((a, b) => a < b ? -1 : a > b ? 1 : 0)) {
3711
+ const map = workspace(doc, key);
3712
+ if (map.get("deleted") === true) continue;
3713
+ const tokens = requireMap(map, "changes").keys().sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
3714
+ const initial = section(doc, "entities").get(key);
3715
+ const { id: _id, kind: _kind, ...initialPayload } = isJsonObject(initial) ? initial : {};
3716
+ const payload = entityContent(doc, key);
3717
+ const changed = tokens.length > 0 || initial !== void 0 && !equalJson(owned(initialPayload), payload);
3718
+ result.set(key, changed ? `entity_${hash([
3719
+ key,
3720
+ tokens,
3721
+ payload
3722
+ ])}` : key);
3723
+ }
3724
+ return result;
3725
+ }
3726
+ function currentId(doc, id, ids = currentIds(doc)) {
3727
+ const resolved = ids.get(family(doc, id));
3728
+ if (resolved === void 0) throw new Error(`Referenced entity ${id} was deleted`);
3729
+ return resolved;
3730
+ }
3731
+ /** Plain domain objects assembled from native Loro state, never a second writable authority. */
3732
+ function readEntityDocument(doc, allowSelectionConflicts = false) {
3733
+ if (root(doc).get("schema") !== "medeo.entities.loro.v1") throw new Error("Document has no Loro entity state");
3734
+ const ids = currentIds(doc);
3735
+ const entities = [...ids].map(([key, entityId]) => {
3736
+ const map = workspace(doc, key);
3737
+ const baseEntityIds = bases(map).map((id) => currentId(doc, id, ids));
3738
+ return {
3739
+ entityId: createEntityId(entityId),
3740
+ entityKind: map.get("kind"),
3741
+ payload: {
3742
+ ...entityContent(doc, key),
3743
+ ...baseEntityIds.length ? { baseEntityIds } : {}
3744
+ }
3745
+ };
3746
+ });
3747
+ const relations = [];
3748
+ for (const key of section(doc, "relations").keys().sort((a, b) => a < b ? -1 : a > b ? 1 : 0)) {
3749
+ const map = section(doc, "relations").get(key);
3750
+ if (!(map instanceof LoroMap) || map.get("deleted") === true) continue;
3751
+ const original = map.get("identity");
3752
+ if (original.endpoints.some((id) => !ids.has(family(doc, id)))) continue;
3753
+ const endpoints = original.endpoints.map((id) => currentId(doc, id, ids));
3754
+ const relationId = equalJson(endpoints, original.endpoints) ? key : `relation_${hash([key, endpoints])}`;
3755
+ relations.push({
3756
+ relationId: createRelationId(relationId),
3757
+ relationKind: original.kind,
3758
+ endpoint0EntityId: createEntityId(endpoints[0]),
3759
+ endpoint1EntityId: createEntityId(endpoints[1]),
3760
+ metadata: requireMap(map, "metadata").toJSON(),
3761
+ trace: requireMap(map, "trace").toJSON()
3762
+ });
3763
+ }
3764
+ const project = {};
3765
+ for (const [key, value] of Object.entries(section(doc, "attachments").toJSON())) {
3766
+ if (typeof value !== "string") throw new Error(`Invalid project attachment ${key}`);
3767
+ project[key] = currentId(doc, value, ids);
3768
+ }
3769
+ const state = {
3770
+ rows: {
3771
+ entities,
3772
+ relations
3773
+ },
3774
+ project
3775
+ };
3776
+ if (!allowSelectionConflicts) for (const [key, id] of ids) {
3777
+ const fingerprint = selectionFingerprint(state.rows, id);
3778
+ if (fingerprint !== void 0 && workspace(doc, key).get("selectionBaseline") !== fingerprint) throw new Error(`Caption text selection conflict at ${id}: source text changed; revise its text ranges explicitly`);
3779
+ }
3780
+ return state;
3781
+ }
3782
+ /** Native editing containers survive domain version changes and merge independently. */
3783
+ var LoroEntityDocument = class LoroEntityDocument {
3784
+ doc;
3785
+ validate;
3786
+ undoManager;
3787
+ constructor(doc, validate) {
3788
+ this.doc = doc;
3789
+ this.validate = validate;
3790
+ this.undoManager = new UndoManager(doc, {
3791
+ mergeInterval: 0,
3792
+ excludeOriginPrefixes: [MATERIALIZE_ORIGIN]
3793
+ });
3794
+ }
3795
+ /** Open a causal snapshot using the same Loro runtime as the compiler. */
3796
+ static fromSnapshot(snapshot, validate) {
3797
+ const doc = new LoroDoc();
3798
+ doc.import(snapshot);
3799
+ readEntityDocument(doc);
3800
+ return new LoroEntityDocument(doc, validate);
3801
+ }
3802
+ static create(rows, project = {}, peerId) {
3803
+ const doc = new LoroDoc();
3804
+ if (peerId !== void 0) doc.setPeerId(peerId);
3805
+ root(doc).set("schema", LORO_ENTITY_SCHEMA);
3806
+ for (const name of [
3807
+ "editing",
3808
+ "entities",
3809
+ "origins",
3810
+ "relations",
3811
+ "project",
3812
+ "attachments",
3813
+ "ordering"
3814
+ ]) root(doc).ensureMergeableMap(name);
3815
+ for (const row of rows.entities) apply(doc, {
3816
+ type: "create",
3817
+ row
3818
+ });
3819
+ for (const row of rows.relations) apply(doc, {
3820
+ type: "relation",
3821
+ row
3822
+ });
3823
+ for (const [slot, entityId] of Object.entries(project)) apply(doc, {
3824
+ type: "attach",
3825
+ slot,
3826
+ entityId
3827
+ });
3828
+ for (const [group, ids] of entityOrderGroups(rows)) writeEntityOrder(doc, group, ids);
3829
+ const initialState = readEntityDocument(doc, true);
3830
+ for (const row of initialState.rows.entities) {
3831
+ const fingerprint = selectionFingerprint(initialState.rows, row.entityId);
3832
+ if (fingerprint !== void 0) workspace(doc, row.entityId).set("selectionBaseline", fingerprint);
3833
+ }
3834
+ assertState(readEntityDocument(doc));
3835
+ materialize(doc);
3836
+ doc.commit({ origin: MATERIALIZE_ORIGIN });
3837
+ return new LoroEntityDocument(doc);
3838
+ }
3839
+ snapshot() {
3840
+ return readEntityDocument(this.doc);
3841
+ }
3842
+ relationsOf(entityId) {
3843
+ const id = currentIds(this.doc).get(family(this.doc, entityId));
3844
+ if (id === void 0) return [];
3845
+ return this.snapshot().rows.relations.filter((row) => row.endpoint0EntityId === id || row.endpoint1EntityId === id);
3846
+ }
3847
+ /** Resolve a queued edit target through its native identity without changing history. */
3848
+ resolveCurrentEntityId(id) {
3849
+ return currentId(this.doc, id);
3850
+ }
3851
+ getVersion(id) {
3852
+ const entity = section(this.doc, "entities").get(id);
3853
+ if (!isJsonObject(entity)) return void 0;
3854
+ const { id: entityId, kind, ...payload } = entity;
3855
+ if (typeof entityId !== "string") throw new Error("Invalid stored entity ID");
3856
+ return {
3857
+ entityId: createEntityId(entityId),
3858
+ entityKind: kind,
3859
+ payload
3860
+ };
3861
+ }
3862
+ /** The callback and every domain validation execute on a causally complete fork first. */
3863
+ transact(edit) {
3864
+ const probe = this.doc.fork();
3865
+ const draft = new LoroEntityDraft(probe);
3866
+ edit(draft);
3867
+ draft.captureSelectionBaselines();
3868
+ const state = readEntityDocument(probe);
3869
+ assertState(state);
3870
+ this.validate?.(state);
3871
+ materialize(probe);
3872
+ const from = this.doc.oplogVersion();
3873
+ for (const command of draft.commands) apply(this.doc, command);
3874
+ this.doc.commit({ origin: "medeo.edit" });
3875
+ materialize(this.doc);
3876
+ this.doc.commit({ origin: MATERIALIZE_ORIGIN });
3877
+ return this.doc.export({
3878
+ mode: "update",
3879
+ from
3880
+ });
3881
+ }
3882
+ /** Import preserves native operation identities; projection errors never discard remote operations. */
3883
+ import(update) {
3884
+ this.doc.import(update);
3885
+ const state = this.snapshot();
3886
+ assertState(state);
3887
+ this.validate?.(state);
3888
+ materialize(this.doc);
3889
+ this.doc.commit({ origin: MATERIALIZE_ORIGIN });
3890
+ }
3891
+ /** Bind a saved snapshot without rewriting text operations or resolving selection conflicts. */
3892
+ bindAudioScriptAsset(entityId, assetId, expectedContentHash) {
3893
+ const key = family(this.doc, entityId);
3894
+ const map = workspace(this.doc, key);
3895
+ if (map.get("kind") !== "audio-script" || map.get("deleted") === true) throw new Error("Resource binding requires an active AudioScript");
3896
+ const payload = entityContent(this.doc, key);
3897
+ if (audioScriptAssetHash(payload) !== expectedContentHash) throw new Error("AudioScript changed while saving its resource");
3898
+ const from = this.doc.oplogVersion();
3899
+ writeObject(requireMap(map, "content"), payload, {
3900
+ ...payload,
3901
+ ...audioScriptAssetFields(payload, assetId)
3902
+ });
3903
+ this.doc.commit({ origin: "medeo.asset-sync" });
3904
+ materialize(this.doc, true);
3905
+ this.doc.commit({ origin: MATERIALIZE_ORIGIN });
3906
+ return this.doc.export({
3907
+ mode: "update",
3908
+ from
3909
+ });
3910
+ }
3911
+ canUndo() {
3912
+ return this.undoManager.canUndo();
3913
+ }
3914
+ canRedo() {
3915
+ return this.undoManager.canRedo();
3916
+ }
3917
+ undo() {
3918
+ return this.historyEdit(() => this.undoManager.undo());
3919
+ }
3920
+ redo() {
3921
+ return this.historyEdit(() => this.undoManager.redo());
3922
+ }
3923
+ historyEdit(run) {
3924
+ const before = new Map(this.snapshot().rows.entities.map((row) => [family(this.doc, row.entityId), row]));
3925
+ if (!run()) return false;
3926
+ const after = this.snapshot();
3927
+ for (const row of after.rows.entities) {
3928
+ const id = family(this.doc, row.entityId);
3929
+ const previous = before.get(id);
3930
+ if (previous && !equalJson(owned(previous.payload), owned(row.payload))) workspace(this.doc, id).ensureMergeableMap("changes").set(globalThis.crypto.randomUUID(), true);
3931
+ }
3932
+ materialize(this.doc);
3933
+ this.doc.commit({ origin: MATERIALIZE_ORIGIN });
3934
+ return true;
3935
+ }
3936
+ };
3937
+ var LoroEntityDraft = class {
3938
+ doc;
3939
+ commands = [];
3940
+ selectedRoots = /* @__PURE__ */ new Set();
3941
+ relationAliases = /* @__PURE__ */ new Map();
3942
+ constructor(doc) {
3943
+ this.doc = doc;
3944
+ for (const row of this.snapshot().rows.relations) this.relationAliases.set(row.relationId, this.relationRoot(row.relationId));
3945
+ }
3946
+ snapshot() {
3947
+ return readEntityDocument(this.doc, true);
3948
+ }
3949
+ order(groupId, entityIds) {
3950
+ this.record({
3951
+ type: "order",
3952
+ group: family(this.doc, groupId),
3953
+ roots: entityIds.map((id) => family(this.doc, id)),
3954
+ token: globalThis.crypto.randomUUID()
3955
+ });
3956
+ }
3957
+ /** Project the final scalar placement intents onto the existing native ordering containers. */
3958
+ reconcileOrder(rows) {
3959
+ const before = new Map([...entityOrderGroups(this.snapshot().rows)].map(([id, ids]) => [family(this.doc, id), ids.map((id) => family(this.doc, id))]));
3960
+ const after = new Map([...entityOrderGroups(rows)].map(([id, ids]) => [family(this.doc, id), ids.map((id) => family(this.doc, id))]));
3961
+ for (const group of new Set([...before.keys(), ...after.keys()])) {
3962
+ const next = after.get(group) ?? [];
3963
+ if (!equalJson(before.get(group) ?? [], next)) this.order(group, next);
3964
+ }
3965
+ }
3966
+ create(row) {
3967
+ this.record({
3968
+ type: "create",
3969
+ row: clone(row)
3970
+ });
3971
+ }
3972
+ delete(entityId) {
3973
+ this.record({
3974
+ type: "delete",
3975
+ root: family(this.doc, entityId)
3976
+ });
3977
+ }
3978
+ attach(slot, entityId) {
3979
+ this.record({
3980
+ type: "attach",
3981
+ slot,
3982
+ entityId
3983
+ });
3984
+ }
3985
+ link(row) {
3986
+ this.record({
3987
+ type: "relation",
3988
+ row: clone(row)
3989
+ });
3990
+ }
3991
+ patchRelation(relationId, metadata, trace) {
3992
+ this.record({
3993
+ type: "relation-fields",
3994
+ root: this.relationRoot(relationId),
3995
+ metadata: clone(metadata),
3996
+ trace: clone(trace)
3997
+ });
3998
+ }
3999
+ unlink(relationId) {
4000
+ this.record({
4001
+ type: "unlink",
4002
+ root: this.relationRoot(relationId)
4003
+ });
4004
+ }
4005
+ relationRoot(relationId) {
4006
+ const alias = this.relationAliases.get(relationId);
4007
+ if (alias !== void 0) return alias;
4008
+ const ids = currentIds(this.doc);
4009
+ for (const key of section(this.doc, "relations").keys()) {
4010
+ const map = section(this.doc, "relations").get(key);
4011
+ if (!(map instanceof LoroMap) || map.get("deleted") === true) continue;
4012
+ const identity = map.get("identity");
4013
+ if (identity.endpoints.some((id) => !ids.has(family(this.doc, id)))) continue;
4014
+ const endpoints = identity.endpoints.map((id) => currentId(this.doc, id, ids));
4015
+ if ((equalJson(endpoints, identity.endpoints) ? key : `relation_${hash([key, endpoints])}`) === relationId || key === relationId) return key;
4016
+ }
4017
+ throw new Error(`Unknown relation ${relationId}`);
4018
+ }
4019
+ update(entityId, fields) {
4020
+ const state = this.snapshot();
4021
+ const id = createEntityId(currentId(this.doc, entityId));
4022
+ const updates = /* @__PURE__ */ new Map();
4023
+ for (const [field, value] of Object.entries(fields)) {
4024
+ const owner = resolveEntityFieldOwner(state.rows, id, field);
4025
+ const payload = updates.get(owner) ?? clone(state.rows.entities.find((row) => row.entityId === owner).payload);
4026
+ updates.set(owner, {
4027
+ ...payload,
4028
+ [field]: clone(value)
4029
+ });
4030
+ }
4031
+ for (const [owner, payload] of updates) this.replaceOwned(owner, payload);
4032
+ }
4033
+ declare(entityId, fields) {
4034
+ const row = this.snapshot().rows.entities.find((row) => row.entityId === currentId(this.doc, entityId));
4035
+ if (!row) throw new Error(`Unknown entity ${entityId}`);
4036
+ this.replaceOwned(entityId, {
4037
+ ...row.payload,
4038
+ ...fields
4039
+ });
4040
+ }
4041
+ replaceOwned(entityId, payload) {
4042
+ this.record({
4043
+ type: "write",
4044
+ root: family(this.doc, entityId),
4045
+ payload: clone(payload),
4046
+ token: globalThis.crypto.randomUUID()
4047
+ });
4048
+ }
4049
+ editText(entityId, path, start, deleteCount, text) {
4050
+ if (typeof path[0] !== "string") throw new Error("Text path must start with a field");
4051
+ const owner = resolveEntityFieldOwner(this.snapshot().rows, createEntityId(currentId(this.doc, entityId)), path[0]);
4052
+ this.record({
4053
+ type: "text",
4054
+ root: family(this.doc, owner),
4055
+ path: [...path],
4056
+ start,
4057
+ deleteCount,
4058
+ text,
4059
+ token: globalThis.crypto.randomUUID()
4060
+ });
4061
+ }
4062
+ /** Generic assembled-field edit compiled at the author's causal position. */
4063
+ change(entityId, changes) {
4064
+ assertFieldChanges(changes);
4065
+ for (const raw of changes) {
4066
+ const change = clone(raw);
4067
+ if (change.path[0] === "baseEntityIds") {
4068
+ if (change.op === "list.insert" && typeof change.value === "string") change.value = currentId(this.doc, change.value);
4069
+ if (change.op === "list.remove" || change.op === "list.move") change.elementId = currentId(this.doc, change.elementId);
4070
+ if ((change.op === "list.insert" || change.op === "list.move") && change.beforeElementId !== null) change.beforeElementId = currentId(this.doc, change.beforeElementId);
4071
+ }
4072
+ const state = this.snapshot();
4073
+ const id = createEntityId(currentId(this.doc, entityId));
4074
+ const owner = resolveEntityFieldOwner(state.rows, id, change.path[0]);
4075
+ const row = state.rows.entities.find((row) => row.entityId === owner);
4076
+ if (equalJson(row.payload, applyFieldChanges(row.payload, [change]))) continue;
4077
+ this.record({
4078
+ type: "field",
4079
+ root: family(this.doc, owner),
4080
+ path: resolveFieldPath(row.payload, change.path),
4081
+ change: clone(change),
4082
+ token: globalThis.crypto.randomUUID()
4083
+ });
4084
+ }
4085
+ }
4086
+ changeRelation(relationId, changes) {
4087
+ assertFieldChanges(changes);
4088
+ const key = this.relationRoot(relationId);
4089
+ const map = section(this.doc, "relations").get(key);
4090
+ if (!(map instanceof LoroMap) || map.get("deleted") === true) throw new Error("Unknown relation");
4091
+ for (const change of changes) {
4092
+ if (!["metadata", "trace"].includes(change.path[0]) || change.path.length < 2) throw new Error("Relation changes may only edit metadata or trace fields");
4093
+ const payload = {
4094
+ metadata: requireMap(map, "metadata").toJSON(),
4095
+ trace: requireMap(map, "trace").toJSON()
4096
+ };
4097
+ applyFieldChanges(payload, [change]);
4098
+ this.record({
4099
+ type: "relation-field",
4100
+ root: key,
4101
+ path: resolveFieldPath(payload, change.path),
4102
+ change: clone(change)
4103
+ });
4104
+ }
4105
+ }
4106
+ captureSelectionBaselines() {
4107
+ const state = this.snapshot();
4108
+ for (const key of this.selectedRoots) {
4109
+ const id = currentIds(this.doc).get(key);
4110
+ if (id === void 0) continue;
4111
+ const fingerprint = selectionFingerprint(state.rows, id);
4112
+ if (fingerprint !== void 0) this.record({
4113
+ type: "selection-baseline",
4114
+ root: key,
4115
+ fingerprint
4116
+ });
4117
+ }
4118
+ }
4119
+ record(command) {
4120
+ if (command.type === "create") this.selectedRoots.add(command.row.entityId);
4121
+ if (command.type === "write" && !equalJson(content(workspace(this.doc, command.root)).selection, command.payload.selection)) this.selectedRoots.add(command.root);
4122
+ if (command.type === "field" && command.path[0] === "selection") this.selectedRoots.add(command.root);
4123
+ apply(this.doc, command);
4124
+ this.commands.push(command);
4125
+ }
4126
+ };
4127
+ function apply(doc, command) {
4128
+ switch (command.type) {
4129
+ case "selection-baseline":
4130
+ workspace(doc, command.root).set("selectionBaseline", command.fingerprint);
4131
+ break;
4132
+ case "order":
4133
+ writeEntityOrder(doc, command.group, command.roots, command.token);
4134
+ break;
4135
+ case "create": {
4136
+ const row = command.row;
4137
+ if (section(doc, "origins").get(row.entityId) !== void 0) throw new Error(`Duplicate entity ${row.entityId}`);
4138
+ const map = section(doc, "editing").ensureMergeableMap(row.entityId);
4139
+ map.set("kind", row.entityKind);
4140
+ map.ensureMergeableMap("changes");
4141
+ section(doc, "origins").set(row.entityId, row.entityId);
4142
+ writeObject(map.ensureMergeableMap("content"), {}, owned(row.payload));
4143
+ writeBases(doc, map, baseIds(row.payload));
4144
+ break;
4145
+ }
4146
+ case "write": {
4147
+ const map = workspace(doc, command.root);
4148
+ const before = content(map);
4149
+ const after = owned(command.payload);
4150
+ if (!equalJson(before, after)) {
4151
+ writeObject(map.ensureMergeableMap("content"), before, after);
4152
+ map.ensureMergeableMap("changes").set(command.token, true);
4153
+ }
4154
+ writeBases(doc, map, baseIds(command.payload));
4155
+ break;
4156
+ }
4157
+ case "text": {
4158
+ const map = workspace(doc, command.root);
4159
+ const text = requireText(map.ensureMergeableMap("content"), command.path);
4160
+ if (!Number.isSafeInteger(command.start) || !Number.isSafeInteger(command.deleteCount) || command.start < 0 || command.deleteCount < 0 || command.start + command.deleteCount > Array.from(text.toString()).length) throw new Error("Invalid text edit range");
4161
+ const start = text.convertPos(command.start, "unicode", "utf16");
4162
+ const end = text.convertPos(command.start + command.deleteCount, "unicode", "utf16");
4163
+ text.splice(start, end - start, command.text);
4164
+ map.ensureMergeableMap("changes").set(command.token, true);
4165
+ break;
4166
+ }
4167
+ case "field": {
4168
+ const map = workspace(doc, command.root);
4169
+ if (command.path[0] === "baseEntityIds") {
4170
+ const before = { baseEntityIds: bases(map).map((id) => currentId(doc, id)) };
4171
+ if (command.change.op === "list.move") throw new Error("Composition bases are unordered; list.move is not applicable");
4172
+ writeBases(doc, map, baseIds(applyFieldChanges(before, [command.change])));
4173
+ } else {
4174
+ applyNativeFieldChange(requireMap(map, "content"), command.path, command.change);
4175
+ map.ensureMergeableMap("changes").set(command.token, true);
4176
+ }
4177
+ break;
4178
+ }
4179
+ case "relation-field": {
4180
+ const map = section(doc, "relations").get(command.root);
4181
+ if (!(map instanceof LoroMap) || map.get("deleted") === true) throw new Error("Unknown relation");
4182
+ applyNativeFieldChange(map, command.path, command.change);
4183
+ break;
4184
+ }
4185
+ case "delete":
4186
+ workspace(doc, command.root).set("deleted", true);
4187
+ break;
4188
+ case "relation": {
4189
+ const row = command.row;
4190
+ const map = section(doc, "relations").ensureMergeableMap(row.relationId);
4191
+ const identity = {
4192
+ kind: row.relationKind,
4193
+ endpoints: [row.endpoint0EntityId, row.endpoint1EntityId]
4194
+ };
4195
+ const previous = map.get("identity");
4196
+ if (previous !== void 0 && !equalJson(previous, identity)) throw new Error("Relation identity cannot change kind or endpoint positions");
4197
+ if (previous === void 0) map.set("identity", identity);
4198
+ if (map.get("deleted") === true) throw new Error("Deleted relation IDs cannot be reused");
4199
+ const metadata = map.ensureMergeableMap("metadata");
4200
+ const trace = map.ensureMergeableMap("trace");
4201
+ writeObject(metadata, metadata.toJSON(), row.metadata);
4202
+ writeObject(trace, trace.toJSON(), row.trace);
4203
+ break;
4204
+ }
4205
+ case "unlink": {
4206
+ const map = section(doc, "relations").get(command.root);
4207
+ if (!(map instanceof LoroMap)) throw new Error("Unknown relation");
4208
+ map.set("deleted", true);
4209
+ break;
4210
+ }
4211
+ case "relation-fields": {
4212
+ const map = section(doc, "relations").get(command.root);
4213
+ if (!(map instanceof LoroMap)) throw new Error("Unknown relation");
4214
+ const metadata = map.ensureMergeableMap("metadata");
4215
+ const trace = map.ensureMergeableMap("trace");
4216
+ writeObject(metadata, metadata.toJSON(), command.metadata);
4217
+ writeObject(trace, trace.toJSON(), command.trace);
4218
+ break;
4219
+ }
4220
+ case "attach":
4221
+ section(doc, "attachments").set(command.slot, command.entityId);
4222
+ break;
4223
+ }
4224
+ }
4225
+ function writeBases(doc, map, ids) {
4226
+ const links = map.ensureMergeableMap("bases");
4227
+ const wanted = new Map(ids.map((id) => {
4228
+ const origin = section(doc, "origins").get(id);
4229
+ return [typeof origin === "string" ? origin : [...currentIds(doc)].find(([, current]) => current === id)?.[0] ?? id, id];
4230
+ }));
4231
+ if (wanted.size !== ids.length) throw new Error("Duplicate base entity family");
4232
+ for (const key of links.keys()) if (!wanted.has(key)) links.delete(key);
4233
+ for (const [key, id] of wanted) if (links.get(key) === void 0) links.set(key, id);
4234
+ }
4235
+ /** Numeric sub-spans must not silently quote different text after a collaborative edit. */
4236
+ function selectionFingerprint(rows, id) {
4237
+ const row = rows.entities.find((row) => row.entityId === id);
4238
+ if (row?.entityKind !== "caption" || !isJsonObject(row.payload.selection)) return void 0;
4239
+ const selection = row.payload.selection;
4240
+ if (selection.textRange === void 0) return void 0;
4241
+ const segments = assembleEntityContent(rows, row.entityId).payload.segments;
4242
+ if (!Array.isArray(segments)) throw new Error("Caption text selection has no source segments");
4243
+ const source = segments.find((segment) => isJsonObject(segment) && segment.segmentId === selection.segmentId);
4244
+ if (!isJsonObject(source) || typeof source.text !== "string") throw new Error("Caption text selection source is missing");
4245
+ return hash([selection, source.text]);
4246
+ }
4247
+ function assertState(state) {
4248
+ for (const row of state.rows.entities) assembleEntityContent(state.rows, row.entityId);
4249
+ const ids = new Set(state.rows.entities.map((row) => String(row.entityId)));
4250
+ for (const row of state.rows.relations) if (!ids.has(row.endpoint0EntityId) || !ids.has(row.endpoint1EntityId)) throw new Error("Relation has a missing endpoint");
4251
+ }
4252
+ function materialize(doc, allowSelectionConflicts = false) {
4253
+ const state = readEntityDocument(doc, allowSelectionConflicts);
4254
+ const byId = section(doc, "entities");
4255
+ const ids = currentIds(doc);
4256
+ for (const [source, id] of ids) {
4257
+ const origins = section(doc, "origins");
4258
+ if (origins.get(id) !== source) origins.set(id, source);
4259
+ }
4260
+ for (const row of state.rows.entities) {
4261
+ const next = {
4262
+ id: row.entityId,
4263
+ kind: row.entityKind,
4264
+ ...row.payload
4265
+ };
4266
+ const previous = byId.get(row.entityId);
4267
+ if (isJsonObject(previous) && !equalJson(owned(previous), owned(next))) throw new Error("Immutable entity version collision");
4268
+ if (!equalJson(previous, next)) byId.set(row.entityId, next);
4269
+ }
4270
+ const project = section(doc, "project");
4271
+ for (const [slot, id] of Object.entries(state.project)) if (project.get(slot) !== id) project.set(slot, id);
4272
+ }
4273
+ //#endregion
4274
+ //#region src/document/mirror-read.ts
4275
+ /**
4276
+ * Project the mirror state (`VideoDocumentDraft`) into the authoritative
4277
+ * `VideoDocument`. The storage shape is isomorphic to the domain shape (RFC 03
4278
+ * §4, reference/17 §4: meta map + a single `tracks` list + part_library), so this
4279
+ * is a near-identity — it reads `time_position` / `fallback_abs_ms` JSON blobs
4280
+ * back into structured values and trims empty strings, nothing more.
4281
+ *
4282
+ * It maps only authoritative facts (RFC 02 §6): `part_id` + `time_position`. The
4283
+ * projection-derived `VideoDraft` read-view (absolute time, `part_aggregations`,
4284
+ * total duration) is solved separately by the cascade, not here.
4285
+ *
4286
+ * It reads in-memory mirror state (O(n) over the document), not a Loro
4287
+ * `toJSON()` FFI rebuild — the cost the prior schema adapter paid on every
4288
+ * `snapshot()`.
4289
+ */
4290
+ function readVideoDocumentFromDraft(draft) {
4291
+ const partLibrary = {};
4292
+ for (const [partId, payload] of recordEntries(draft.part_library)) {
4293
+ const part = draftToPartUnion(payload);
4294
+ if (part != null) partLibrary[partId] = part;
4295
+ }
4296
+ const timeline = draft.timeline;
4297
+ const hasTimeline = timeline?.unit_time_ms != null;
4298
+ const meta = draft.meta;
4299
+ return {
4300
+ meta: {
4301
+ schema_version: meta?.schema_version === "video-document/entity-projection-v1" ? ENTITY_TIMELINE_PROJECTION_SCHEMA_VERSION : VIDEO_DOCUMENT_SCHEMA_VERSION,
4302
+ draft_id: emptyToUndefined(meta?.draft_id),
4303
+ project_id: emptyToUndefined(meta?.project_id),
4304
+ owner_id: emptyToUndefined(meta?.owner_id),
4305
+ thumbnail_storage_key: emptyToUndefined(meta?.thumbnail_storage_key),
4306
+ chat_session_id: emptyToUndefined(meta?.chat_session_id),
4307
+ video_creation_settings: meta?.video_creation_settings ?? void 0,
4308
+ version: meta?.version ?? void 0
4309
+ },
4310
+ timeline: hasTimeline ? { unit_time_ms: timeline.unit_time_ms } : void 0,
4311
+ tracks: rowsToTracks(draft.tracks),
4312
+ part_library: partLibrary
4313
+ };
4314
+ }
4315
+ /** Map a movable-list of track rows (`$cid`-bearing) into domain tracks, dropping empty-id placeholders. */
4316
+ function rowsToTracks(rows) {
4317
+ return (rows ?? []).filter((row) => row.id != null && row.id !== "").map(rowToTrack);
4318
+ }
4319
+ function rowToTrack(row) {
4320
+ return {
4321
+ id: row.id,
4322
+ parts_kind: row.parts_kind ?? void 0,
4323
+ is_hidden: row.is_hidden ?? void 0,
4324
+ items: (row.items ?? []).map((item) => {
4325
+ const result = {
4326
+ part_id: item.part_id ?? "",
4327
+ time_position: item.time_position
4328
+ };
4329
+ if (item.fallback_abs_ms != null) result.fallback_abs_ms = item.fallback_abs_ms;
4330
+ return result;
4331
+ })
4332
+ };
4333
+ }
4334
+ function emptyToUndefined(value) {
4335
+ return value == null || value === "" ? void 0 : value;
4336
+ }
4337
+ //#endregion
4338
+ //#region src/document/mirror-adapter.ts
4339
+ /**
4340
+ * Storage-layer adapter that backs a `VideoDocument` with `loro-mirror` (ADR
4341
+ * 0008). The mirror holds an in-memory immutable state synced to the `LoroDoc`
4342
+ * by declarative diff, replacing the hand-rolled `@mengine/schema` adapter:
4343
+ *
4344
+ * - `snapshot()` projects the mirror's in-memory state to the read model — no
4345
+ * per-call `toJSON()` FFI rebuild.
4346
+ * - `transact(edit, audit)` runs the whole op in one `mirror.setState` callback:
4347
+ * one diff, one `doc.commit` carrying the audit message. The callback edits an
4348
+ * immer draft, so a throw inside it discards the draft and never touches Loro
4349
+ * (natural rollback) — no `guard` / `rollback` / `openTransaction` machinery.
4350
+ * - mirror's `idSelector` (track items keyed by `part_id`) diffs reorders to
4351
+ * real Loro `move` ops on every lane, so per-item CRDT identity survives on
4352
+ * main and secondary tracks alike.
4353
+ */
4354
+ var MirrorVideoDocumentAdapter = class {
4355
+ doc;
4356
+ mirror;
4357
+ constructor(doc) {
4358
+ this.doc = doc;
4359
+ this.mirror = new Mirror({
4360
+ doc,
4361
+ schema: videoDocumentMirrorSchema
4362
+ });
4363
+ }
4364
+ snapshot() {
4365
+ const document = readVideoDocumentFromDraft(this.mirror.getState());
4366
+ if (this.doc.getMap("medeo").get("schema") === "medeo.entities.loro.v1") return projectEntityTimeline(readEntityDocument(this.doc).rows, document.meta);
4367
+ return document;
4368
+ }
4369
+ /**
4370
+ * True once the doc holds real document content. A fresh mirror over an empty
4371
+ * doc still reports defaulted root maps, so probe the stored `schema_version`
4372
+ * (empty until a snapshot is bootstrapped or synced in).
4373
+ */
4374
+ hasContent() {
4375
+ return (this.mirror.getState().meta?.schema_version ?? "") !== "";
4376
+ }
4377
+ /**
4378
+ * Apply one op as a single transaction. `edit` mutates the immer draft; mirror
4379
+ * diffs the result and commits once with the audit `message`. A throw in `edit`
4380
+ * discards the draft (Loro untouched). When `edit` produces no change, mirror
4381
+ * skips the commit — matching the prior "empty op leaves no audit" behavior.
4382
+ */
4383
+ transact(edit, audit) {
4384
+ if (this.snapshot().meta.schema_version === "video-document/entity-projection-v1") throw new Error("Entity timeline projections are read-only; edit the Medeo entities instead");
4385
+ this.mirror.setState((draft) => {
4386
+ edit(draft);
4387
+ }, {
4388
+ origin: "mengine.semantic_editor",
4389
+ message: JSON.stringify({
4390
+ semantic_op: audit.kind,
4391
+ payload: audit.payload,
4392
+ intent: audit.intent ?? null,
4393
+ actor: audit.actor ?? null
4394
+ })
4395
+ });
4396
+ }
4397
+ };
4398
+ /** Build a fresh Loro doc seeded with `document` through the mirror. */
4399
+ function createMirrorVideoDocument(document, options = {}) {
4400
+ assertValidVideoDocument(document);
4401
+ const doc = new LoroDoc();
4402
+ if (options.peerId != null) doc.setPeerId(options.peerId);
4403
+ new Mirror({
4404
+ doc,
4405
+ schema: videoDocumentMirrorSchema
4406
+ }).setState((draft) => {
4407
+ writeVideoDocumentToDraft(draft, document);
4408
+ }, { origin: options.origin ?? "mengine.bootstrap" });
4409
+ return doc;
4410
+ }
4411
+ /** Build a `MirrorVideoDocumentAdapter` over a fresh doc seeded with `document`. */
4412
+ function createMirrorVideoDocumentAdapter(document, options) {
4413
+ return new MirrorVideoDocumentAdapter(createMirrorVideoDocument(document, options));
4414
+ }
4415
+ /** Host-only replacement of a derived view, retaining the existing Loro history. */
4416
+ function applyEntityTimelineProjection(doc, projection, entityRevision) {
4417
+ if (projection.meta.schema_version !== "video-document/entity-projection-v1") throw new Error("Expected an entity timeline projection");
4418
+ if (!Number.isSafeInteger(entityRevision) || entityRevision < 1) throw new Error("Expected a positive entity revision");
4419
+ assertValidVideoDocument(projection);
4420
+ const from = doc.oplogVersion();
4421
+ new Mirror({
4422
+ doc,
4423
+ schema: videoDocumentMirrorSchema
4424
+ }).setState((draft) => writeVideoDocumentToDraft(draft, {
4425
+ ...projection,
4426
+ meta: {
4427
+ ...projection.meta,
4428
+ version: entityRevision
4429
+ }
4430
+ }), {
4431
+ origin: "mengine.entity_projection",
4432
+ message: JSON.stringify({
4433
+ semantic_op: "EntityTimelineProjection",
4434
+ entity_revision: entityRevision
4435
+ })
4436
+ });
4437
+ return doc.export({
4438
+ mode: "update",
4439
+ from
4440
+ });
4441
+ }
4442
+ /** Write a whole `VideoDocument` into a draft (seed a fresh doc / plain-memory state). */
4443
+ function writeVideoDocumentToDraft(draft, document) {
4444
+ draft.meta = {
4445
+ schema_version: document.meta.schema_version,
4446
+ draft_id: document.meta.draft_id ?? void 0,
4447
+ project_id: document.meta.project_id ?? void 0,
4448
+ owner_id: document.meta.owner_id ?? void 0,
4449
+ thumbnail_storage_key: document.meta.thumbnail_storage_key ?? void 0,
4450
+ chat_session_id: document.meta.chat_session_id ?? void 0,
4451
+ video_creation_settings: document.meta.video_creation_settings ?? void 0,
4452
+ version: document.meta.version ?? void 0
4453
+ };
4454
+ draft.timeline = { unit_time_ms: document.timeline?.unit_time_ms ?? void 0 };
4455
+ draft.tracks = (document.tracks ?? []).map(toTrackRow);
4456
+ draft.part_library = {};
4457
+ for (const [partId, part] of Object.entries(document.part_library ?? {})) draft.part_library[partId] = partUnionToDraft(part);
4458
+ }
4459
+ /** Map a domain `Track` to a draft track row (no lane/lane_order — see schema). */
4460
+ function toTrackRow(track) {
4461
+ return {
4462
+ id: track.id ?? "",
4463
+ parts_kind: track.parts_kind ?? void 0,
4464
+ is_hidden: track.is_hidden ?? void 0,
4465
+ items: (track.items ?? []).map((item) => ({
4466
+ part_id: item.part_id,
4467
+ time_position: item.time_position,
4468
+ fallback_abs_ms: item.fallback_abs_ms
4469
+ }))
4470
+ };
4471
+ }
4472
+ //#endregion
4473
+ //#region src/editor/id-gen.ts
4474
+ /**
4475
+ * Part-id generation, aligned with the online ecosystem.
4476
+ *
4477
+ * The authoritative online producers — agent-harness (`@harness/shared`
4478
+ * `genObjId`) and director.v2 (`common/obj_id.py` `gen_obj_id`) — both mint part
4479
+ * ids as `` `${prefix}_${ulid()}` ``, and real captured drafts use exactly that
4480
+ * shape (`clip_…` / `spe_…` / `cap_…` / `bgm_…`, each a 26-char ULID). The engine
4481
+ * previously emitted `vc_<base36 timestamp><6 random>`, a different prefix AND a
4482
+ * different encoding — the sole cross-repo id divergence. This module removes it
4483
+ * by emitting the same `<prefix>_<ULID>` bytes.
4484
+ *
4485
+ * The ULID is generated inline (Crockford Base32, 48-bit time + 80-bit random)
4486
+ * rather than pulling the `ulid` npm package: the randomness class matches the
4487
+ * old generator (both `Math.random`-based) and it keeps `@mengine/medeo-client`
4488
+ * dependency-free for a purely mechanical id string. Part ids only need to be
4489
+ * unique and lexicographically time-sortable, which this satisfies.
4490
+ */
4491
+ /** Crockford Base32 alphabet (no I, L, O, U), per the ULID spec. */
4492
+ const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
4493
+ const TIME_LEN = 10;
4494
+ const RANDOM_LEN = 16;
4495
+ function encodeTime(now) {
4496
+ let out = "";
4497
+ let ms = now;
4498
+ for (let i = TIME_LEN - 1; i >= 0; i--) {
4499
+ const mod = ms % 32;
4500
+ out = CROCKFORD[mod] + out;
4501
+ ms = (ms - mod) / 32;
4502
+ }
4503
+ return out;
4504
+ }
4505
+ function encodeRandom() {
4506
+ let out = "";
4507
+ for (let i = 0; i < RANDOM_LEN; i++) out += CROCKFORD[Math.floor(Math.random() * 32)];
4508
+ return out;
4509
+ }
4510
+ /** A 26-char Crockford Base32 ULID (10-char time + 16-char random). */
4511
+ function ulid() {
4512
+ return encodeTime(Date.now()) + encodeRandom();
4513
+ }
4514
+ function generatePartId(prefix) {
4515
+ return `${prefix}_${ulid()}`;
4516
+ }
4517
+ //#endregion
4518
+ //#region src/document/plain-memory-adapter.ts
4519
+ /**
4520
+ * Pure in-memory `SemanticDocumentAdapter` — no Loro/WASM. Holds a
4521
+ * `VideoDocumentDraft` object and applies each `transact` via immer `produce`,
4522
+ * journaling every audit that actually mutated state (plus any ids minted
4523
+ * during that transact).
4524
+ *
4525
+ * Known benign difference vs `MirrorVideoDocumentAdapter`: the editor's
4526
+ * `setPart` assigns a fresh part object on every call, so a same-value rewrite
4527
+ * produces a new immer state and IS journaled here, while the mirror's deep
4528
+ * diff emits no commit. Terminal `snapshot()` stays equal; replay is idempotent.
4529
+ */
4530
+ var PlainMemoryAdapter = class {
4531
+ state;
4532
+ _journal = [];
4533
+ baseIdFactory;
4534
+ /** Non-null only while a `transact` edit callback is running. */
4535
+ pendingIds = null;
4536
+ /**
4537
+ * Recording wrapper around the underlying factory. Callers (sandbox editor)
4538
+ * use this so every minted id is appended to the current transact's list.
4539
+ */
4540
+ idFactory;
4541
+ constructor(document, options) {
4542
+ assertValidVideoDocument(document);
4543
+ const draft = {};
4544
+ writeVideoDocumentToDraft(draft, document);
4545
+ this.state = draft;
4546
+ this.baseIdFactory = options?.idFactory ?? generatePartId;
4547
+ this.idFactory = (prefix) => {
4548
+ const id = this.baseIdFactory(prefix);
4549
+ if (this.pendingIds != null) this.pendingIds.push(id);
4550
+ return id;
4551
+ };
4552
+ }
4553
+ get journal() {
4554
+ return this._journal;
4555
+ }
4556
+ hasContent() {
4557
+ return (this.state.meta?.schema_version ?? "") !== "";
4558
+ }
4559
+ snapshot() {
4560
+ return readVideoDocumentFromDraft(this.state);
4561
+ }
4562
+ /**
4563
+ * Apply one op via immer. A throw in `edit` discards the draft (state and
4564
+ * journal unchanged). When `produce` returns the same reference, there was
4565
+ * no structural change — skip journal, matching mirror "no change, no commit".
4566
+ */
4567
+ transact(edit, audit) {
4568
+ if (this.state.meta?.schema_version === "video-document/entity-projection-v1") throw new Error("Entity timeline projections are read-only; edit the Medeo entities instead");
4569
+ this.pendingIds = [];
4570
+ try {
4571
+ const next = produce(this.state, edit);
4572
+ if (next !== this.state) {
4573
+ this.state = next;
4574
+ this._journal.push({
4575
+ ...audit,
4576
+ generated_ids: this.pendingIds
4577
+ });
4578
+ }
4579
+ } finally {
4580
+ this.pendingIds = null;
4581
+ }
4582
+ }
4583
+ };
4584
+ /** Build a `PlainMemoryAdapter` seeded with `document`. */
4585
+ function createPlainMemoryAdapter(document, options) {
4586
+ return new PlainMemoryAdapter(document, options);
4587
+ }
4588
+ //#endregion
4589
+ export { ensureLaneTrack as $, assembleEntityContent as A, hasSequence as B, projectEntityTimeline as C, base64ToBytes as Ct, decodeEntityRelationRows as D, EntityTimelineProjectionError as E, updateEntityFields as F, fromVideoDocument as G, buildInitialVideoDocument as H, variantBaseEntityIds as I, assertValidVideoDocument as J, toVideoDocument as K, isJsonObject as L, assembleScriptText as M, findComposedAudioScript as N, ScriptCompositionError as O, selectAudioScriptSegment as P, LANE_KINDS_IN_STACK_ORDER as Q, createEntityId as R, resolveFieldPath as S, videoDocumentMirrorSchema as St, resolveEntityTimelineLayout as T, buildSpeechHostMap as U, isMediaAssetVariantKind as V, derivePositionFromAbs as W, partUnionSchema as X, validateVideoDocument as Y, videoDocumentSchema as Z, audioScriptAssetHash as _, VIDEO_DOCUMENT_SCHEMA_VERSION as _t, applyEntityTimelineProjection as a, fillMainTrackTimeGaps as at, applyFieldChanges as b, partUnionToDraft as bt, writeVideoDocumentToDraft as c, resolveAllSpeechOverlaps as ct, LoroEntityDocument as d, TIMELINE_SKELETON_DURATION_MS as dt, findLaneTrack as et, LoroEntityDraft as f, isEmptyVideoClip as ft, audioScriptAssetFields as g, ENTITY_TIMELINE_PROJECTION_SCHEMA_VERSION as gt, audioScriptAssetContent as h, DEFAULT_UNIT_TIME_MS as ht, MirrorVideoDocumentAdapter as i, arrangeMainTrackSeamlessly as it, assemblePhoneticScriptContent as j, assembleCaptionContent as k, readVideoDocumentFromDraft as l, resolveSpeechOverlapByShiftingVideos as lt, entityOrderGroups as m, safeDurationMs as mt, createPlainMemoryAdapter as n, solveVideoDocument as nt, createMirrorVideoDocument as o, reassignSpeechesToVideoClipsByTime as ot, readEntityDocument as p, partDurationMs as pt, VideoDocumentValidationError as q, generatePartId as r, cascadeAfterVideoClipChanges as rt, createMirrorVideoDocumentAdapter as s, recalculateTimelineDuration as st, PlainMemoryAdapter as t, laneTrackId as tt, LORO_ENTITY_SCHEMA as u, syncAggregatedClipsTimePosition as ut, hasAudioScriptAsset as v, effectiveVideoClipDurationMs as vt, EntityProjectionGraph as w, bytesToBase64 as wt, assertFieldChanges as x, recordEntries as xt, equalJson as y, speedOf as yt, createRelationId as z };