@mengine/medeo-client 1.2.1-alpha.1 → 1.2.1-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1666 +0,0 @@
1
- import { Mirror, schema } from "loro-mirror";
2
- import { z } from "zod";
3
- import { LoroDoc } from "loro-crdt";
4
- //#region src/client/base64.ts
5
- function bytesToBase64(bytes) {
6
- let binary = "";
7
- for (const byte of bytes) binary += String.fromCharCode(byte);
8
- return btoa(binary);
9
- }
10
- function base64ToBytes(base64) {
11
- const binary = atob(base64);
12
- const bytes = new Uint8Array(binary.length);
13
- for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
14
- return bytes;
15
- }
16
- //#endregion
17
- //#region src/document/mirror-schema.ts
18
- /**
19
- * Declarative `loro-mirror` schema for `VideoDocument` — the canonical structural
20
- * SSOT and live storage wire (replaces the hand-rolled `@mengine/schema`
21
- * definition + adapter, see ADR 0008).
22
- *
23
- * Mirror gives an in-memory immutable state synced to the Loro doc by declarative
24
- * diff, so the storage layer no longer hand-writes per-region reconcile or a
25
- * transaction state machine. The shapes here store only authoritative facts
26
- * (RFC 02 §6): per-item absolute time, `part_aggregations`, and total duration
27
- * are projection-derived (cascade-solved at read time), so they are absent here.
28
- *
29
- * - tracks live in a single `tracks` `LoroMovableList` keyed by track id
30
- * (reference/17 §4). Lane is expressed by each track's `parts_kind`, not by
31
- * which container it lives in; order is the movable-list order itself. There is
32
- * no `lane` / `lane_order` field, so two peers inserting tracks never collide
33
- * on an order integer — list moves are conflict-free (RFC 03 §4). The legacy
34
- * keyed-map (`LoroMapRecord` + an integer `lane_order`) and the prior three
35
- * named containers are gone; the `main` / `above` / `below` three-pane view is
36
- * rebuilt by the projection from `parts_kind`.
37
- * - each track's `items` is a `LoroMovableList` keyed by `part_id`: a part is
38
- * placed at most once per lane, so `part_id` is the stable placement identity.
39
- * Reorder diffs to a real Loro `move`, preserving per-item CRDT identity on
40
- * every lane (main and secondary alike).
41
- * - `time_position` is the authoritative positioning fact (RFC 02 §4,
42
- * reference/17 §3), carried as an opaque JSON blob string (a whole tagged-union
43
- * value, last-writer-wins). The derived `fallback_abs_ms` snapshot sits beside
44
- * it as its own number field — a separate LWW unit so a projection refresh
45
- * never clobbers a `time_position` edit.
46
- * - meta lives in a `meta` LoroMap: the mirror root only accepts container
47
- * schemas, so meta scalars can't sit at the root as bare values. The domain
48
- * `VideoDocument` keeps the same `meta` wrapper, so the shapes are isomorphic
49
- * and the read/seed mapping is a near-identity.
50
- * - `part_library` values are nested `LoroMap`s: the value is a `LoroMap` with
51
- * four mutually-exclusive optional part sub-maps (`video_clip` / `speech` /
52
- * `caption` / `bgm`), each a `LoroMap` whose fields are stored as real Loro
53
- * sub-keys. This makes each field its own CRDT unit, so two peers editing
54
- * different fields of the same part (e.g. one volume, one play_out) merge
55
- * field-by-field instead of one whole-value overwrite. Field `required` mirrors
56
- * the Smithy `@required` contract (see `video_draft_comp.smithy`). `kind` is not
57
- * stored (the present sub-key names the kind); `duration_ms` is not stored (it
58
- * is derived, reference/17 §5). Nested values that the engine does not edit
59
- * field-by-field — `speed_shift` (a tagged union), `voice`, `caption_ids` — stay
60
- * as opaque JSON-blob sub-keys via `transform`. The four sub-keys are optional,
61
- * so the "exactly one part kind" union invariant is not enforced by the schema
62
- * type; it is rebuilt by `draftToPartUnion` on read and gated by zod on write.
63
- * - a caption's `style` is the one lazily-created optional child container, so
64
- * `captionPart` sets `mergeableMapChildContainers: true` — two peers can
65
- * concurrently first-create the same caption's `style` (one attribute each), and
66
- * only a mergeable child converges instead of last-writer-wins dropping one. All
67
- * other nested maps are keyed by a unique id (`part_library`) or created
68
- * atomically with their parent (`partValue`'s part sub-map), so they never hit
69
- * that concurrent-first-create fork and stay on plain `setContainer`. See the
70
- * note on `captionPart` below.
71
- * - `video_creation_settings` is an opaque JSON blob carried in a string via
72
- * `transform`: a whole value, last-writer-wins (no field-level concurrent edits).
73
- */
74
- /**
75
- * JSON-blob transform for an opaque, last-writer-wins value carried in a Loro
76
- * string. The field must be declared `required: false`: an absent field decodes
77
- * to `undefined` (mirror never calls `decode`/`encode` for null/undefined),
78
- * which is how we represent "no value" instead of encoding a `null` sentinel.
79
- */
80
- function jsonTransform() {
81
- return {
82
- decode: (value) => JSON.parse(value),
83
- encode: (value) => JSON.stringify(value),
84
- isEqual: "encoded-value-equality"
85
- };
86
- }
87
- const trackItem = schema.LoroMap({
88
- part_id: schema.String(),
89
- time_position: schema.String().transform(jsonTransform()),
90
- fallback_abs_ms: schema.Number({ required: false }),
91
- duration_override_ms: schema.Number({ required: false })
92
- });
93
- const track = schema.LoroMap({
94
- id: schema.String(),
95
- parts_kind: schema.String({ required: false }),
96
- is_hidden: schema.Boolean({ required: false }),
97
- items: schema.LoroMovableList(trackItem, (item) => item.part_id)
98
- });
99
- const videoClipPart = schema.LoroMap({
100
- id: schema.String(),
101
- play_in: schema.Number(),
102
- play_out: schema.Number(),
103
- volume: schema.Number(),
104
- origin_media_id: schema.String(),
105
- speed_shift: schema.String({ required: false }).transform(jsonTransform())
106
- }, { required: false });
107
- const speechPart = schema.LoroMap({
108
- id: schema.String(),
109
- media_duration_ms: schema.Number(),
110
- audio_script: schema.String(),
111
- volume: schema.Number(),
112
- audio_storage_key: schema.String(),
113
- origin_speech_id: schema.String(),
114
- voice: schema.String().transform(jsonTransform()),
115
- caption_ids: schema.String().transform(jsonTransform())
116
- }, { required: false });
117
- const captionStyle = schema.LoroMap({
118
- font_id: schema.String({ required: false }),
119
- font_size: schema.Number({ required: false }),
120
- font_color: schema.String({ required: false }),
121
- font_weight: schema.Number({ required: false }),
122
- entrance_animation: schema.String({ required: false }),
123
- entrance_animation_duration_ms: schema.Number({ required: false }),
124
- stroke_color: schema.String({ required: false }),
125
- stroke_width: schema.Number({ required: false }),
126
- position_x: schema.Number({ required: false }),
127
- position_y: schema.Number({ required: false })
128
- }, { required: false });
129
- const captionPart = schema.LoroMap({
130
- id: schema.String(),
131
- initial_duration_ms: schema.Number(),
132
- speech_part_id: schema.String(),
133
- text: schema.String(),
134
- start_ms: schema.Number(),
135
- style: captionStyle
136
- }, {
137
- required: false,
138
- mergeableMapChildContainers: true
139
- });
140
- const bgmPart = schema.LoroMap({
141
- id: schema.String(),
142
- audio_storage_key: schema.String(),
143
- volume: schema.Number(),
144
- origin_media_id: schema.String()
145
- }, { required: false });
146
- const partValue = schema.LoroMap({
147
- video_clip: videoClipPart,
148
- speech: speechPart,
149
- caption: captionPart,
150
- bgm: bgmPart
151
- });
152
- const videoDocumentMirrorSchema = schema({
153
- meta: schema.LoroMap({
154
- schema_version: schema.String({ required: false }),
155
- draft_id: schema.String({ required: false }),
156
- project_id: schema.String({ required: false }),
157
- owner_id: schema.String({ required: false }),
158
- thumbnail_storage_key: schema.String({ required: false }),
159
- chat_session_id: schema.String({ required: false }),
160
- video_creation_settings: schema.String({ required: false }).transform(jsonTransform()),
161
- version: schema.Number({ required: false })
162
- }),
163
- timeline: schema.LoroMap({ unit_time_ms: schema.Number({ required: false }) }),
164
- tracks: schema.LoroMovableList(track, (t) => t.id),
165
- part_library: schema.LoroMapRecord(partValue)
166
- });
167
- /** Project a discriminated `PartUnion` into the draft's four-optional shape. */
168
- function partUnionToDraft(part) {
169
- if (part.video_clip != null) return { video_clip: omitKind(part.video_clip) };
170
- if (part.speech != null) return { speech: omitKind(part.speech) };
171
- if (part.caption != null) return { caption: omitKind(part.caption) };
172
- return { bgm: omitKind(part.bgm) };
173
- }
174
- /**
175
- * Rebuild a discriminated `PartUnion` from a draft part value: pick the one
176
- * present sub-key, restore its `kind`, and drop the `$cid`. Returns `undefined`
177
- * when no part sub-key is present (an empty/placeholder value).
178
- */
179
- function draftToPartUnion(value) {
180
- if (value == null) return void 0;
181
- const v = value;
182
- if (v.video_clip != null) return { video_clip: withKind(v.video_clip, "video_clip") };
183
- if (v.speech != null) return { speech: withKind(v.speech, "speech") };
184
- if (v.caption != null) return { caption: withKind(v.caption, "caption") };
185
- if (v.bgm != null) return { bgm: withKind(v.bgm, "bgm") };
186
- }
187
- /** Drop `kind` (not stored) and `$cid` from a part payload for the draft. */
188
- function omitKind(part) {
189
- const { kind: _kind, $cid: _cid, ...rest } = part;
190
- return rest;
191
- }
192
- /** Restore `kind` (and drop `$cid`) on a part sub-map read from the draft. */
193
- function withKind(part, kind) {
194
- const { $cid: _cid, ...rest } = part;
195
- return {
196
- ...rest,
197
- kind
198
- };
199
- }
200
- /** Entries of a draft record (e.g. `tracks`) with `$cid` stripped. */
201
- function recordEntries(record) {
202
- if (record == null) return [];
203
- const out = [];
204
- for (const [key, value] of Object.entries(record)) {
205
- if (key === "$cid") continue;
206
- out.push([key, value]);
207
- }
208
- return out;
209
- }
210
- //#endregion
211
- //#region src/document/types.ts
212
- const VIDEO_DOCUMENT_SCHEMA_VERSION = "video-document/v0";
213
- /** The linear speed multiplier of a `speed_shift`, defaulting to 1 (original). */
214
- function speedOf(speedShift) {
215
- const speed = speedShift?.config?.linear?.speed;
216
- return typeof speed === "number" && Number.isFinite(speed) && speed > 0 ? speed : 1;
217
- }
218
- /**
219
- * A video clip's effective timeline duration, derived from authoritative facts
220
- * (RFC 02, `reference/16` §4, reference/17 §5): the trim window
221
- * `play_out - play_in` divided by the speed multiplier, rounded to integer ms.
222
- * `play_in` / `play_out` are optional in the IDL but every write path sets them
223
- * (defaulting to the whole media), and the legacy-ingest projection backfills the
224
- * window from a legacy `duration_ms` — so an authoritative clip always carries a
225
- * trim window and there is no stored `duration_ms` to fall back to. A clip with
226
- * neither bound yields 0. Speeds the clip up (>1× → shorter) or down (<1×).
227
- */
228
- function effectiveVideoClipDurationMs(clip) {
229
- const speed = speedOf(clip.speed_shift);
230
- const sourceMs = (clip.play_out ?? 0) - (clip.play_in ?? 0);
231
- const effective = Math.round(sourceMs / speed);
232
- return Number.isFinite(effective) && effective > 0 ? effective : 0;
233
- }
234
- /**
235
- * The `timeline.unit_time_ms` the read-view states when the document states none.
236
- *
237
- * One frame at 30fps. A document legitimately has no such fact (display
238
- * granularity is the editor's, not the timeline's), but the read-view IDL marks
239
- * the field `@required`, so the projection has to name a value. This one is a
240
- * usable granularity; `0` — which a downstream consumer had been defaulting to —
241
- * is not, and passes any `>= 0` guard on its way to an editor that cannot use it.
242
- */
243
- const DEFAULT_UNIT_TIME_MS = 33.333;
244
- //#endregion
245
- //#region src/timeline-core/types.ts
246
- /** Total document duration when there is no real content (matches FE bgm fallback). */
247
- const TIMELINE_SKELETON_DURATION_MS = 2e4;
248
- /** Empty placeholder clip marker: a video_clip part with no backing media. */
249
- function isEmptyVideoClip(part) {
250
- const clip = part?.video_clip;
251
- if (clip == null) return false;
252
- return clip.origin_media_id == null || clip.origin_media_id === "";
253
- }
254
- /** Clamp a duration to a non-negative integer (NaN/Infinity/negative → 0). */
255
- function safeDurationMs(value) {
256
- const n = Number(value);
257
- if (!Number.isFinite(n) || n <= 0) return 0;
258
- return Math.round(n);
259
- }
260
- function partDurationMs(doc, partId) {
261
- const part = doc.part_library[partId];
262
- if (part?.video_clip != null) return safeDurationMs(effectiveVideoClipDurationMs(part.video_clip));
263
- if (part?.bgm != null) return doc.timeline.duration_ms > 0 ? doc.timeline.duration_ms : TIMELINE_SKELETON_DURATION_MS;
264
- if (part?.speech != null) return safeDurationMs(part.speech.media_duration_ms);
265
- if (part?.caption != null) return safeDurationMs(part.caption.initial_duration_ms);
266
- return 0;
267
- }
268
- //#endregion
269
- //#region src/timeline-core/cascade.ts
270
- /**
271
- * Canonical timeline cascade primitives (ADR 0009).
272
- *
273
- * Single source of truth for "how an edit's connected regions move": main-track
274
- * seamless layout, aggregation position sync + reassignment, total-duration
275
- * recompute, speech-overlap resolution, gap filling. Reconciled per ADR 0009 §4:
276
- *
277
- * - product behavior follows the FE current implementation;
278
- * - all times are integer ms — positions/durations are rounded, never floated;
279
- * - function decomposition follows agent-harness (the Python-derived structure).
280
- *
281
- * Every function mutates the `TimelineDoc` in place (the editor runs them inside
282
- * one immer `transact`, so in-place edits diff correctly).
283
- */
284
- /**
285
- * Lay the main track out head-to-tail from 0, rewriting each item's
286
- * `abs_time_position`. Items whose part is missing from the library are dropped.
287
- */
288
- function arrangeMainTrackSeamlessly(doc) {
289
- const kept = [];
290
- let cursor = 0;
291
- for (const item of doc.main_track) {
292
- if (doc.part_library[item.part_id] == null) continue;
293
- item.abs_time_position = cursor;
294
- cursor += partDurationMs(doc, item.part_id);
295
- kept.push(item);
296
- }
297
- doc.main_track = kept;
298
- }
299
- /**
300
- * Sync attached parts' absolute positions from their host:
301
- * speech.abs = host_video.abs + relative_time_position
302
- * caption.abs = speech.abs + caption.start_ms
303
- */
304
- function syncAggregatedClipsTimePosition(doc) {
305
- const videoByPart = indexByPart(doc.main_track);
306
- const speechByPart = indexByPart(doc.speech_track);
307
- for (const aggregation of doc.aggregations) {
308
- const videoItem = videoByPart.get(aggregation.body_part_id);
309
- if (videoItem == null) continue;
310
- for (const attachment of aggregation.attachments) {
311
- const speechItem = speechByPart.get(attachment.part_id);
312
- if (speechItem == null) continue;
313
- speechItem.abs_time_position = videoItem.abs_time_position + attachment.relative_time_position;
314
- }
315
- }
316
- for (const captionItem of doc.caption_track) {
317
- const captionPart = doc.part_library[captionItem.part_id]?.caption;
318
- if (captionPart == null) continue;
319
- const speechItem = captionPart.speech_part_id == null ? void 0 : speechByPart.get(captionPart.speech_part_id);
320
- if (speechItem == null) continue;
321
- captionItem.abs_time_position = speechItem.abs_time_position + safeDurationMs(captionPart.start_ms);
322
- }
323
- }
324
- /**
325
- * Reassign each speech to the video clip whose time range contains its start,
326
- * rebuilding `aggregations`. A speech before the first clip or after the last
327
- * falls back to the first / last clip respectively (FE: see §4 note — FE falls
328
- * back to last only; we keep the harness two-sided fallback because a speech
329
- * dragged before clip 0 belonging to the last clip is clearly wrong, and the FE
330
- * single-sided rule is an acknowledged rough edge). `relative_time_position` is
331
- * clamped to a non-negative integer.
332
- */
333
- function reassignSpeechesToVideoClipsByTime(doc) {
334
- const ranges = doc.main_track.filter((item) => doc.part_library[item.part_id] != null).map((item) => ({
335
- part_id: item.part_id,
336
- start: item.abs_time_position,
337
- end: item.abs_time_position + partDurationMs(doc, item.part_id)
338
- }));
339
- if (ranges.length === 0) {
340
- doc.aggregations = [];
341
- return;
342
- }
343
- const rebuilt = /* @__PURE__ */ new Map();
344
- for (const speechItem of doc.speech_track) {
345
- const start = speechItem.abs_time_position;
346
- const targetRange = ranges.find((r) => r.start <= start && start < r.end) ?? (start < ranges[0].start ? ranges[0] : ranges[ranges.length - 1]);
347
- const relative = Math.max(0, Math.round(start - targetRange.start));
348
- const targetId = targetRange.part_id;
349
- let aggregation = rebuilt.get(targetId);
350
- if (aggregation == null) {
351
- aggregation = {
352
- body_part_id: targetId,
353
- attachments: []
354
- };
355
- rebuilt.set(targetId, aggregation);
356
- }
357
- aggregation.attachments.push({
358
- part_id: speechItem.part_id,
359
- relative_time_position: relative
360
- });
361
- }
362
- doc.aggregations = ranges.map((r) => rebuilt.get(r.part_id)).filter((a) => a != null);
363
- }
364
- /**
365
- * Recompute `timeline.duration_ms` as the max end (abs + duration) across main,
366
- * speech, and caption lanes (BGM does not extend the timeline).
367
- *
368
- * BGM has no authoritative duration (RFC 02 / `reference/16` §0b): its effective
369
- * length is always the timeline total, so it is not written back here — the
370
- * projection derives it from `timeline.duration_ms` on read (`partDurationMs`
371
- * returns the timeline total for a bgm part). The empty-document 20s skeleton is
372
- * applied at that read step, not stored.
373
- */
374
- function recalculateTimelineDuration(doc) {
375
- const max = Math.max(laneEndMs(doc, doc.main_track), laneEndMs(doc, doc.speech_track), laneEndMs(doc, doc.caption_track));
376
- doc.timeline.duration_ms = max;
377
- }
378
- /**
379
- * Resolve one speech overlap by shifting the overlapping speech's host video
380
- * (and every clip after it) right. Returns true when one overlap was resolved;
381
- * callers loop until it returns false. The compared range is the speech merged
382
- * with its captions: start = min(speech.start, captions.start) (FE behavior),
383
- * end = max(speech.end, captions.end).
384
- */
385
- function resolveSpeechOverlapByShiftingVideos(doc) {
386
- const speeches = doc.speech_track;
387
- for (let i = 1; i < speeches.length; i++) {
388
- const prev = speechWithCaptionsRange(doc, speeches[i - 1]);
389
- const curr = speechWithCaptionsRange(doc, speeches[i]);
390
- if (prev == null || curr == null) continue;
391
- if (curr.start >= prev.end) continue;
392
- const overlapMs = Math.round(prev.end - curr.start);
393
- const hostId = hostVideoOf(doc, speeches[i].part_id);
394
- if (hostId == null) continue;
395
- const fromIndex = doc.main_track.findIndex((item) => item.part_id === hostId);
396
- if (fromIndex < 0) continue;
397
- for (let idx = fromIndex; idx < doc.main_track.length; idx++) doc.main_track[idx].abs_time_position += overlapMs;
398
- syncAggregatedClipsTimePosition(doc);
399
- return true;
400
- }
401
- return false;
402
- }
403
- /**
404
- * Resolve speech overlaps for one cascade pass. Mirrors the authoritative FE
405
- * `ensureNoOverlappingClips`, which is documented to resolve AT MOST ONE overlap
406
- * per cascade and is invoked exactly once at every FE call site — the supported
407
- * ops each produce at most one new overlap. It is NOT a fixpoint loop: shifting a
408
- * host right also moves every speech anchored to it, so two speeches sharing a
409
- * host can never be separated by shifting. Looping to a "fixed point" there does
410
- * not converge — it accumulates the same overlap every iteration and pushes the
411
- * clip arbitrarily far right (e.g. a sped-up clip landing at ~287k ms instead of
412
- * its seamless slot). A single pass matches FE product behavior and terminates.
413
- */
414
- function resolveAllSpeechOverlaps(doc) {
415
- resolveSpeechOverlapByShiftingVideos(doc);
416
- }
417
- /**
418
- * Make the main track gapless by adjusting/merging empty placeholder clips or
419
- * inserting new ones between real clips. Mirrors the harness four-case rule, but
420
- * the merge of two adjacent empty clips keeps the earlier clip (harness Case 4).
421
- * Requires `makeEmptyPart` to mint a placeholder part (the editor supplies an
422
- * id generator).
423
- */
424
- function fillMainTrackTimeGaps(doc, makeEmptyPart) {
425
- const items = doc.main_track;
426
- let i = 0;
427
- while (i < items.length) {
428
- const current = items[i];
429
- const currentPart = doc.part_library[current.part_id];
430
- if (currentPart == null) {
431
- i += 1;
432
- continue;
433
- }
434
- const currentEnd = current.abs_time_position + partDurationMs(doc, current.part_id);
435
- if (i + 1 >= items.length) break;
436
- const next = items[i + 1];
437
- const nextPart = doc.part_library[next.part_id];
438
- if (nextPart == null) {
439
- i += 1;
440
- continue;
441
- }
442
- const gap = next.abs_time_position - currentEnd;
443
- if (gap > 0) {
444
- if (isEmptyVideoClip(currentPart)) {
445
- extendEmptyClip(currentPart.video_clip, gap);
446
- continue;
447
- }
448
- if (isEmptyVideoClip(nextPart)) {
449
- next.abs_time_position -= gap;
450
- extendEmptyClip(nextPart.video_clip, gap);
451
- i += 1;
452
- continue;
453
- }
454
- const { partId } = makeEmptyPart(gap);
455
- doc.part_library[partId] = { video_clip: {
456
- id: partId,
457
- kind: "video_clip",
458
- play_in: 0,
459
- play_out: gap,
460
- volume: 0,
461
- origin_media_id: ""
462
- } };
463
- items.splice(i + 1, 0, {
464
- part_id: partId,
465
- time_position: { mode: "sequential" },
466
- abs_time_position: currentEnd
467
- });
468
- i += 1;
469
- continue;
470
- }
471
- if (gap === 0 && isEmptyVideoClip(currentPart) && isEmptyVideoClip(nextPart)) {
472
- extendEmptyClip(currentPart.video_clip, partDurationMs(doc, next.part_id));
473
- items.splice(i + 1, 1);
474
- delete doc.part_library[next.part_id];
475
- continue;
476
- }
477
- i += 1;
478
- }
479
- }
480
- function indexByPart(items) {
481
- const map = /* @__PURE__ */ new Map();
482
- for (const item of items) map.set(item.part_id, item);
483
- return map;
484
- }
485
- function laneEndMs(doc, items) {
486
- let max = 0;
487
- for (const item of items) {
488
- if (doc.part_library[item.part_id] == null) continue;
489
- const end = item.abs_time_position + partDurationMs(doc, item.part_id);
490
- if (end > max) max = end;
491
- }
492
- return max;
493
- }
494
- function extendEmptyClip(clip, byMs) {
495
- clip.play_out = safeDurationMs(clip.play_out) + byMs;
496
- }
497
- /** speech merged with its captions: start = min, end = max. */
498
- function speechWithCaptionsRange(doc, speechItem) {
499
- const speech = doc.part_library[speechItem.part_id]?.speech;
500
- if (speech == null) return null;
501
- let start = speechItem.abs_time_position;
502
- let end = speechItem.abs_time_position + safeDurationMs(speech.media_duration_ms);
503
- for (const captionId of captionIdsOf(speech)) {
504
- const captionItem = doc.caption_track.find((item) => item.part_id === captionId);
505
- const captionPart = doc.part_library[captionId]?.caption;
506
- if (captionItem == null || captionPart == null) continue;
507
- const cStart = captionItem.abs_time_position;
508
- const cEnd = captionItem.abs_time_position + safeDurationMs(captionPart.initial_duration_ms);
509
- if (cStart < start) start = cStart;
510
- if (cEnd > end) end = cEnd;
511
- }
512
- return {
513
- start,
514
- end
515
- };
516
- }
517
- function captionIdsOf(speech) {
518
- return (speech.caption_ids ?? []).filter((id) => id != null);
519
- }
520
- function hostVideoOf(doc, speechPartId) {
521
- for (const aggregation of doc.aggregations) if (aggregation.attachments.some((att) => att.part_id === speechPartId)) return aggregation.body_part_id;
522
- return null;
523
- }
524
- //#endregion
525
- //#region src/timeline-core/entrypoints.ts
526
- /**
527
- * The full solve pipeline: arrange → sync → reassign → resolve-overlap →
528
- * fill-gaps → recalc. The single cascade the read-side projection runs; ops
529
- * never call it (they write only facts — RFC 02 §7/§10).
530
- */
531
- function cascadeAfterVideoClipChanges(doc, makeEmptyPart) {
532
- arrangeMainTrackSeamlessly(doc);
533
- syncAggregatedClipsTimePosition(doc);
534
- reassignSpeechesToVideoClipsByTime(doc);
535
- resolveAllSpeechOverlaps(doc);
536
- fillMainTrackTimeGaps(doc, makeEmptyPart);
537
- recalculateTimelineDuration(doc);
538
- }
539
- //#endregion
540
- //#region src/timeline-core/bridge.ts
541
- /**
542
- * Solve a `VideoDocument` (authoritative, position-only) into its derived
543
- * read-view: absolute time per item, `part_aggregations`, and total duration.
544
- * This is the read side of the single-directional flow — never written back.
545
- */
546
- function solveVideoDocument(document) {
547
- const doc = videoDocumentToTimelineDoc(document);
548
- let counter = 0;
549
- const derivedFillerPartIds = /* @__PURE__ */ new Set();
550
- cascadeAfterVideoClipChanges(doc, () => {
551
- const partId = `empty_${counter++}`;
552
- derivedFillerPartIds.add(partId);
553
- return { partId };
554
- });
555
- const absByPartId = /* @__PURE__ */ new Map();
556
- for (const item of doc.main_track) absByPartId.set(item.part_id, item.abs_time_position);
557
- for (const item of doc.speech_track) absByPartId.set(item.part_id, item.abs_time_position);
558
- for (const item of doc.caption_track) absByPartId.set(item.part_id, item.abs_time_position);
559
- for (const item of doc.bgm_track) absByPartId.set(item.part_id, item.abs_time_position);
560
- return {
561
- absByPartId,
562
- aggregations: doc.aggregations.map((aggregation) => ({
563
- body_part_id: aggregation.body_part_id,
564
- attachments: aggregation.attachments.map((a) => ({
565
- part_id: a.part_id,
566
- relative_time_position: a.relative_time_position
567
- }))
568
- })),
569
- durationMs: doc.timeline.duration_ms,
570
- partLibrary: doc.part_library,
571
- derivedFillerPartIds
572
- };
573
- }
574
- function videoDocumentToTimelineDoc(document) {
575
- const partLibrary = {};
576
- for (const [partId, part] of Object.entries(document.part_library ?? {})) partLibrary[partId] = part;
577
- const laneItems = (track) => (track?.items ?? []).map((item) => ({
578
- part_id: item.part_id,
579
- time_position: item.time_position,
580
- abs_time_position: seedAbs(item.time_position, item.fallback_abs_ms),
581
- fallback_abs_ms: item.fallback_abs_ms
582
- }));
583
- const tracks = document.tracks ?? [];
584
- let mainItems = [];
585
- let speechItems = [];
586
- let bgmItems = [];
587
- let captionItems = [];
588
- for (const t of tracks) switch (t.parts_kind) {
589
- case "video_clip":
590
- mainItems = mainItems.concat(laneItems(t));
591
- break;
592
- case "speech":
593
- speechItems = speechItems.concat(laneItems(t));
594
- break;
595
- case "bgm":
596
- bgmItems = bgmItems.concat(laneItems(t));
597
- break;
598
- case "caption":
599
- captionItems = captionItems.concat(laneItems(t));
600
- break;
601
- }
602
- return {
603
- main_track: mainItems,
604
- speech_track: speechItems,
605
- caption_track: captionItems,
606
- bgm_track: bgmItems,
607
- part_library: partLibrary,
608
- aggregations: seedAggregations(mainItems, speechItems),
609
- timeline: {
610
- duration_ms: 0,
611
- unit_time_ms: document.timeline?.unit_time_ms ?? 0
612
- }
613
- };
614
- }
615
- /**
616
- * Seed an item's solve-variable `abs_time_position` from its `time_position` (and
617
- * the `fallback_abs_ms` snapshot carried beside it): `absolute` → `offsetMs`;
618
- * `anchored` → `fallbackAbsMs` (last solved position, also the orphan-recovery
619
- * anchor when the host is gone); `sequential` → 0 (the cascade lays the main
620
- * track out head-to-tail). The cascade then resolves anchored items against their
621
- * host, so this seed only needs to put each item somewhere plausible for the
622
- * first reassign/overlap pass.
623
- */
624
- function seedAbs(position, fallbackAbsMs) {
625
- switch (position.mode) {
626
- case "absolute": return Math.round(position.offsetMs);
627
- case "anchored": return Math.round(fallbackAbsMs ?? 0);
628
- case "sequential": return 0;
629
- }
630
- }
631
- /**
632
- * Build the cascade `aggregations` parent-map from anchored items: an anchored
633
- * item's `anchorPartId` is the host's `part_id` (= `body_part_id`), `offsetMs`
634
- * is `relative_time_position`. Only main-track (video) hosts form aggregations;
635
- * caption→speech relations are recomputed by the cascade from `caption.start_ms`.
636
- */
637
- function seedAggregations(mainItems, speechItems) {
638
- const mainPartIds = new Set(mainItems.map((item) => item.part_id));
639
- const byHost = /* @__PURE__ */ new Map();
640
- const order = [];
641
- for (const speech of speechItems) {
642
- if (speech.time_position.mode !== "anchored") continue;
643
- const host = speech.time_position.anchorPartId;
644
- if (!mainPartIds.has(host)) continue;
645
- let aggregation = byHost.get(host);
646
- if (aggregation == null) {
647
- aggregation = {
648
- body_part_id: host,
649
- attachments: []
650
- };
651
- byHost.set(host, aggregation);
652
- order.push(host);
653
- }
654
- aggregation.attachments.push({
655
- part_id: speech.part_id,
656
- relative_time_position: speech.time_position.offsetMs
657
- });
658
- }
659
- return order.map((host) => byHost.get(host));
660
- }
661
- /**
662
- * The four lanes in top-to-bottom stack order — the order a `tracks` list holds
663
- * them in (see {@link laneRank}).
664
- *
665
- * Exported so a document can be seeded with all four lanes up front. That seed is
666
- * not cosmetic: {@link ensureLaneTrack} is find-then-mint over a `LoroMovableList`,
667
- * so two concurrent writers that each mint the same absent lane both keep their
668
- * row, and the merged document holds two tracks for one lane. For the main lane
669
- * that is fatal — `videoDocumentSchema` allows at most one `video_clip` track, so
670
- * the merged document stops being projectable at all, symmetrically on both
671
- * replicas. Pre-seeding every lane makes `ensureLaneTrack` always take its find
672
- * branch, which removes the race by construction rather than by detection.
673
- *
674
- * What closes the race is that a track with the lane's `parts_kind` EXISTS — the
675
- * lookup is by kind, not by id. So a seed is only safe if it covers every lane:
676
- * a partial seed leaves the uncovered lanes exactly as exposed as before.
677
- */
678
- const LANE_KINDS_IN_STACK_ORDER = [
679
- "caption",
680
- "video_clip",
681
- "speech",
682
- "bgm"
683
- ];
684
- /**
685
- * The conventional track id for a lane (`main_track`, `<kind>_track`). Shared by
686
- * the up-front seed and {@link ensureLaneTrack}'s lazy mint so a document's lane
687
- * ids do not depend on which of the two created the track.
688
- *
689
- * Ids are cosmetic to the merge itself — lane lookup goes by `parts_kind`, so
690
- * drifting them apart would not reopen the concurrent-mint race. They matter to
691
- * readers that address a lane by id (the FE editor's panes, fixtures), which is
692
- * why there is one convention rather than two.
693
- */
694
- function laneTrackId(kind) {
695
- return kind === "video_clip" ? "main_track" : `${kind}_track`;
696
- }
697
- /**
698
- * Lane-stacking rank for the single `tracks` list (reference/17 §4): caption
699
- * (above) sits before the video_clip main track, which sits before speech / bgm
700
- * (below). The `tracks` array is kept in this top-to-bottom order so a freshly
701
- * minted track lands in the right place and the projection's three-pane rebuild
702
- * stays deterministic.
703
- */
704
- function laneRank(kind) {
705
- switch (kind) {
706
- case "caption": return 0;
707
- case "video_clip": return 1;
708
- case "speech":
709
- case "bgm": return 2;
710
- default: return 3;
711
- }
712
- }
713
- /**
714
- * Insert a freshly-minted track into `tracks` at the position that keeps the
715
- * lane-stacking order (caption → main → speech/bgm). Inserts before the first
716
- * track whose rank is strictly greater, so same-rank tracks keep insertion order.
717
- */
718
- function insertTrackByLaneOrder(tracks, track) {
719
- const rank = laneRank(track.parts_kind);
720
- const at = tracks.findIndex((t) => laneRank(t?.parts_kind) > rank);
721
- if (at < 0) tracks.push(track);
722
- else tracks.splice(at, 0, track);
723
- }
724
- /**
725
- * Locate a lane's track row in the single `tracks` list by kind, minting an empty
726
- * row in lane-stacking order if absent (reference/17 §4: lane = `parts_kind`).
727
- * Ops use this to write authoritative items onto the right lane. The track id
728
- * comes from {@link laneTrackId}, shared with the up-front seed.
729
- *
730
- * The mint branch is a concurrency hazard, not a convenience: see
731
- * {@link LANE_KINDS_IN_STACK_ORDER}. A document seeded with all four lanes never
732
- * reaches it.
733
- */
734
- function ensureLaneTrack(draft, kind) {
735
- draft.tracks ??= [];
736
- let track = draft.tracks.find((t) => t?.parts_kind === kind);
737
- if (track == null) {
738
- track = {
739
- id: laneTrackId(kind),
740
- parts_kind: kind,
741
- is_hidden: void 0,
742
- items: []
743
- };
744
- insertTrackByLaneOrder(draft.tracks, track);
745
- }
746
- track.items ??= [];
747
- return track;
748
- }
749
- /** Find a secondary lane's track row without minting it. */
750
- function findLaneTrack(draft, kind) {
751
- return (draft.tracks ?? []).find((t) => t?.parts_kind === kind);
752
- }
753
- //#endregion
754
- //#region src/document/zod-schema.ts
755
- const partKindSchema = z.enum([
756
- "video_clip",
757
- "speech",
758
- "caption",
759
- "bgm"
760
- ]);
761
- const finiteNumber = z.number().finite();
762
- const trackItemTimePositionSchema = z.discriminatedUnion("mode", [
763
- z.object({ mode: z.literal("sequential") }).passthrough(),
764
- z.object({
765
- mode: z.literal("anchored"),
766
- anchorPartId: z.string(),
767
- offsetMs: finiteNumber
768
- }).passthrough(),
769
- z.object({
770
- mode: z.literal("absolute"),
771
- offsetMs: finiteNumber
772
- }).passthrough()
773
- ]);
774
- const trackItemSchema = z.object({
775
- part_id: z.string(),
776
- time_position: trackItemTimePositionSchema,
777
- fallback_abs_ms: finiteNumber.optional(),
778
- duration_override_ms: z.number().finite().positive().optional()
779
- }).passthrough();
780
- const trackSchema = z.object({
781
- id: z.string().optional(),
782
- parts_kind: partKindSchema.optional(),
783
- is_hidden: z.boolean().optional(),
784
- items: z.array(trackItemSchema).optional()
785
- }).passthrough();
786
- const speedShiftSchema = z.object({
787
- category: z.string().optional(),
788
- mode: z.string().optional(),
789
- config: z.object({ linear: z.object({ speed: finiteNumber.optional() }).passthrough().optional() }).passthrough().optional()
790
- }).passthrough();
791
- const videoClipPartSchema = z.object({
792
- id: z.string().optional(),
793
- kind: z.literal("video_clip"),
794
- duration_ms: finiteNumber.optional(),
795
- play_in: finiteNumber,
796
- play_out: finiteNumber,
797
- volume: finiteNumber,
798
- origin_media_id: z.string(),
799
- speed_shift: speedShiftSchema.optional()
800
- }).passthrough();
801
- const speechPartSchema = z.object({
802
- id: z.string().optional(),
803
- kind: z.literal("speech"),
804
- media_duration_ms: finiteNumber,
805
- duration_ms: finiteNumber.optional(),
806
- audio_script: z.string(),
807
- volume: finiteNumber,
808
- audio_storage_key: z.string(),
809
- origin_speech_id: z.string(),
810
- voice: z.unknown(),
811
- caption_ids: z.array(z.string())
812
- }).passthrough();
813
- const captionPartSchema = z.object({
814
- id: z.string().optional(),
815
- kind: z.literal("caption"),
816
- initial_duration_ms: finiteNumber,
817
- speech_part_id: z.string(),
818
- text: z.string(),
819
- start_ms: finiteNumber,
820
- style: z.object({
821
- font_id: z.string().optional(),
822
- font_size: finiteNumber.optional(),
823
- font_color: z.string().optional(),
824
- font_weight: finiteNumber.optional(),
825
- entrance_animation: z.string().optional(),
826
- entrance_animation_duration_ms: finiteNumber.optional(),
827
- stroke_color: z.string().optional(),
828
- stroke_width: finiteNumber.optional(),
829
- position_x: finiteNumber.optional(),
830
- position_y: finiteNumber.optional()
831
- }).passthrough().optional()
832
- }).passthrough();
833
- const bgmPartSchema = z.object({
834
- id: z.string().optional(),
835
- kind: z.literal("bgm"),
836
- audio_storage_key: z.string(),
837
- volume: finiteNumber,
838
- origin_media_id: z.string()
839
- }).passthrough();
840
- const partUnionSchema = z.union([
841
- z.object({ video_clip: videoClipPartSchema }).passthrough(),
842
- z.object({ speech: speechPartSchema }).passthrough(),
843
- z.object({ caption: captionPartSchema }).passthrough(),
844
- z.object({ bgm: bgmPartSchema }).passthrough()
845
- ]).refine((part) => {
846
- const p = part;
847
- return [
848
- "video_clip",
849
- "speech",
850
- "caption",
851
- "bgm"
852
- ].filter((k) => p[k] != null).length === 1;
853
- }, { message: "A part_library value must have exactly one of video_clip / speech / caption / bgm" });
854
- const videoDocumentMetaSchema = z.object({
855
- schema_version: z.literal(VIDEO_DOCUMENT_SCHEMA_VERSION),
856
- draft_id: z.string().optional(),
857
- project_id: z.string().optional(),
858
- owner_id: z.string().optional(),
859
- thumbnail_storage_key: z.string().optional(),
860
- chat_session_id: z.string().optional(),
861
- video_creation_settings: z.unknown().optional(),
862
- version: finiteNumber.optional()
863
- }).passthrough();
864
- const videoDocumentSchema = z.object({
865
- meta: videoDocumentMetaSchema,
866
- timeline: z.object({ unit_time_ms: finiteNumber.optional() }).passthrough().optional(),
867
- tracks: z.array(trackSchema).optional(),
868
- part_library: z.record(z.string(), partUnionSchema).optional()
869
- }).passthrough().refine((doc) => (doc.tracks ?? []).filter((t) => t.parts_kind === "video_clip").length <= 1, {
870
- message: "A VideoDocument may have at most one video_clip (main) track",
871
- path: ["tracks"]
872
- });
873
- //#endregion
874
- //#region src/document/validation.ts
875
- /**
876
- * Business-level schema guard for `VideoDocument` (RFC 03 §9). It is the gate
877
- * that decides whether an arbitrary value is a *legal* `VideoDocument` before it
878
- * is written into Loro — distinct from two neighbours:
879
- *
880
- * - `zod-schema.ts` (`videoDocumentSchema`) checks structure/shape only; this
881
- * file layers the business rules on top (part-kind match, reference integrity,
882
- * `position.anchorPartId` targets, value ranges, identity uniqueness).
883
- * - loro-mirror's own `validateSchema` (run on every `setState`) only checks the
884
- * storage structure, never these business invariants.
885
- *
886
- * Projection (`projection.ts`) calls `assertValidVideoDocument` before solving a
887
- * document into the legacy `VideoDraft`.
888
- */
889
- var VideoDocumentValidationError = class extends Error {
890
- issues;
891
- constructor(issues) {
892
- super(`Invalid VideoDocument: ${issues.map((issue) => issue.message).join("; ")}`);
893
- this.issues = issues;
894
- this.name = "VideoDocumentValidationError";
895
- }
896
- };
897
- /**
898
- * Assert the document is legal enough to project. Only `error`-severity issues
899
- * hard-reject; `recoverable` ones (dangling anchor / orphan, RFC 02 §11.1) are
900
- * left for the projection to heal on read and do NOT throw. Use
901
- * `validateVideoDocument` directly to inspect recoverable issues too.
902
- */
903
- function assertValidVideoDocument(document) {
904
- const blocking = validateVideoDocument(document).filter((issue) => (issue.severity ?? "error") === "error");
905
- if (blocking.length > 0) throw new VideoDocumentValidationError(blocking);
906
- }
907
- function validateVideoDocument(document) {
908
- const parsed = videoDocumentSchema.safeParse(document);
909
- if (!parsed.success) return parsed.error.issues.map((issue) => ({
910
- code: "invalid_schema",
911
- path: zodPath(issue.path),
912
- message: issue.message
913
- }));
914
- const doc = parsed.data;
915
- const issues = [];
916
- const partLibrary = doc.part_library ?? {};
917
- for (const [partId, part] of Object.entries(partLibrary)) {
918
- if (!partUnionSchema.safeParse(part).success) {
919
- issues.push({
920
- code: "unknown_part_kind",
921
- path: `/part_library/${partId}`,
922
- message: `Part "${partId}" is not a supported part union`
923
- });
924
- continue;
925
- }
926
- const wrapperKind = getPartUnionKind(part);
927
- const innerKind = getPartKind(part);
928
- if (wrapperKind != null && innerKind != null && wrapperKind !== innerKind) issues.push({
929
- code: "part_kind_mismatch",
930
- path: `/part_library/${partId}`,
931
- message: `Part "${partId}" wrapper kind "${wrapperKind}" does not match inner kind "${innerKind}"`
932
- });
933
- validatePartValues(partId, part, issues);
934
- }
935
- for (const [idx, track] of (doc.tracks ?? []).entries()) validateTrack(track, `tracks/${idx}`, partLibrary, issues, track.parts_kind === "video_clip");
936
- validateLaneTrackUniqueness(doc, issues);
937
- validateSpeechCaptionReferences(partLibrary, issues);
938
- validatePositionReferences(doc, partLibrary, issues);
939
- return issues;
940
- }
941
- /**
942
- * Report a duplicated lane: two tracks sharing the same `id`, or two secondary
943
- * tracks of the same `parts_kind`.
944
- *
945
- * Found by the Phase 6 M0/S2 concurrency probe. `ensureLaneTrack` locates a lane
946
- * by `parts_kind` and mints `<kind>_track` when absent, so two replicas that both
947
- * start without (say) a caption track each create one; `tracks` is a
948
- * `LoroMovableList`, so the merge keeps BOTH. The lane is then split across two
949
- * same-id tracks, and the projection's `find`-by-kind returns only one of them —
950
- * so an item can be authoritative yet invisible in the pane a reader looks at.
951
- *
952
- * Flagged **recoverable**, not `error`, for two reasons. It is a reachable
953
- * outcome of ordinary concurrent editing, and hard-rejecting would make an
954
- * unavoidable merge result un-projectable — the same argument §11.1 makes for
955
- * orphans. And the projection already tolerates it: `solveVideoDocument`
956
- * concatenates every track of a kind, so the cascade sees all items regardless.
957
- * The defect is a *reader-visible* split, so the right response is to surface it
958
- * (loudly, in a channel someone reads), not to block the document.
959
- *
960
- * The main track is exempt from the kind check: `parts_kind === 'video_clip'` may
961
- * legitimately appear on several tracks (the projection treats only the first as
962
- * the main pane), so only its `id` collision is reported.
963
- */
964
- function validateLaneTrackUniqueness(doc, issues) {
965
- const tracks = doc.tracks ?? [];
966
- const seenIds = /* @__PURE__ */ new Set();
967
- const seenSecondaryKinds = /* @__PURE__ */ new Set();
968
- for (const [idx, track] of tracks.entries()) {
969
- const id = track?.id;
970
- if (id != null && id !== "") if (seenIds.has(id)) issues.push({
971
- code: "duplicate_lane_track",
972
- path: `/tracks/${idx}/id`,
973
- message: `Track "tracks/${idx}" reuses track id "${id}"`,
974
- severity: "recoverable"
975
- });
976
- else seenIds.add(id);
977
- const kind = track?.parts_kind;
978
- if (kind == null || kind === "video_clip") continue;
979
- if (seenSecondaryKinds.has(kind)) issues.push({
980
- code: "duplicate_lane_track",
981
- path: `/tracks/${idx}/parts_kind`,
982
- message: `Lane "${kind}" is split across more than one track (at "tracks/${idx}")`,
983
- severity: "recoverable"
984
- });
985
- else seenSecondaryKinds.add(kind);
986
- }
987
- }
988
- /**
989
- * An `anchored` item whose `anchorPartId` no longer exists in the library is the
990
- * orphan condition (RFC 02 §4/§11.1) — e.g. a speech whose host video, or a
991
- * caption whose host speech, was concurrently deleted while this item was being
992
- * reparented in. This is flagged as a **recoverable** issue, NOT a hard error:
993
- * the projection heals it on read (the item's `fallback_abs_ms` snapshot seeds
994
- * its absolute position, then the cascade reassigns it to the nearest available
995
- * host, or it stays put as an absolute item). Reporting it here keeps the orphan
996
- * observable without blocking projection — the opposite of a hard reject, which
997
- * would make an inevitable concurrent-delete outcome un-projectable (§11.1).
998
- */
999
- function validatePositionReferences(doc, partLibrary, issues) {
1000
- const tracks = (doc.tracks ?? []).map((track, idx) => ({
1001
- track,
1002
- path: `tracks/${idx}`
1003
- }));
1004
- for (const { track, path } of tracks) for (const [idx, item] of (track?.items ?? []).entries()) {
1005
- if (item.time_position.mode !== "anchored") continue;
1006
- if (partLibrary[item.time_position.anchorPartId] == null) issues.push({
1007
- code: "invalid_position_anchor",
1008
- path: `/${path}/items/${idx}/time_position/anchorPartId`,
1009
- message: `Track item "${path}/${idx}" anchors to missing part "${item.time_position.anchorPartId}"`,
1010
- severity: "recoverable"
1011
- });
1012
- }
1013
- }
1014
- function validatePartValues(partId, part, issues) {
1015
- const payload = part.video_clip ?? part.speech ?? part.caption ?? part.bgm ?? void 0;
1016
- if (payload == null) return;
1017
- if ("duration_ms" in payload && payload.duration_ms != null && payload.duration_ms < 0) issues.push({
1018
- code: "invalid_part_value",
1019
- path: `/part_library/${partId}/duration_ms`,
1020
- message: `Part "${partId}" has negative duration_ms`
1021
- });
1022
- if ("volume" in payload && payload.volume != null && !Number.isFinite(payload.volume)) issues.push({
1023
- code: "invalid_part_value",
1024
- path: `/part_library/${partId}/volume`,
1025
- message: `Part "${partId}" has non-finite volume`
1026
- });
1027
- if (part.video_clip != null) {
1028
- const { play_in, play_out } = part.video_clip;
1029
- if (play_in != null && play_in < 0) issues.push({
1030
- code: "invalid_part_value",
1031
- path: `/part_library/${partId}/play_in`,
1032
- message: `Video clip "${partId}" has negative play_in`
1033
- });
1034
- if (play_out != null && play_in != null && play_out < play_in) issues.push({
1035
- code: "invalid_part_value",
1036
- path: `/part_library/${partId}/play_out`,
1037
- message: `Video clip "${partId}" has play_out before play_in`
1038
- });
1039
- }
1040
- }
1041
- function validateTrack(track, path, partLibrary, issues, isMainTrack) {
1042
- if (track == null) return;
1043
- const seenPartIds = /* @__PURE__ */ new Set();
1044
- for (const [idx, item] of (track.items ?? []).entries()) {
1045
- const partId = item.part_id;
1046
- if (partId != null && partId !== "") if (seenPartIds.has(partId)) issues.push({
1047
- code: "duplicate_track_item_identity",
1048
- path: `/${path}/items/${idx}/part_id`,
1049
- message: `Track item "${path}/${idx}" reuses part_id "${partId}"`
1050
- });
1051
- else seenPartIds.add(partId);
1052
- if (partId == null || partId === "") {
1053
- issues.push({
1054
- code: "missing_part_reference",
1055
- path: `/${path}/items/${idx}/part_id`,
1056
- message: `Track item "${path}/${idx}" has no part_id`
1057
- });
1058
- continue;
1059
- }
1060
- const part = partLibrary[partId];
1061
- if (part == null) {
1062
- issues.push({
1063
- code: "missing_part_reference",
1064
- path: `/${path}/items/${idx}/part_id`,
1065
- message: `Track item "${path}/${idx}" references missing part "${partId}"`
1066
- });
1067
- continue;
1068
- }
1069
- const partKind = getPartKind(part);
1070
- if (isMainTrack && partKind !== "video_clip") issues.push({
1071
- code: "main_track_non_video_clip",
1072
- path: `/${path}/items/${idx}/part_id`,
1073
- message: `Main track item "${path}/${idx}" references non-video part "${partId}"`
1074
- });
1075
- if (track.parts_kind != null && partKind != null && track.parts_kind !== partKind) issues.push({
1076
- code: "track_kind_mismatch",
1077
- path: `/${path}/items/${idx}/part_id`,
1078
- message: `Track "${path}" expects "${track.parts_kind}" but part "${partId}" is "${partKind}"`
1079
- });
1080
- }
1081
- }
1082
- /**
1083
- * Check speech↔caption references. A reference to a *missing* part (speech's
1084
- * caption_ids → deleted caption, or caption's speech_part_id → deleted speech)
1085
- * is the orphan condition (RFC 02 §11.1): reported as **recoverable** so the
1086
- * projection can heal it on read, not block. A *back-pointer mismatch* (caption
1087
- * exists but does not point back) is data corruption, kept as a hard error.
1088
- */
1089
- function validateSpeechCaptionReferences(partLibrary, issues) {
1090
- for (const [partId, part] of Object.entries(partLibrary)) {
1091
- if (part.speech != null) for (const captionId of part.speech.caption_ids ?? []) {
1092
- const caption = partLibrary[captionId]?.caption;
1093
- if (caption == null) {
1094
- issues.push({
1095
- code: "invalid_speech_caption_reference",
1096
- path: `/part_library/${partId}/speech/caption_ids`,
1097
- message: `Speech "${partId}" references missing caption "${captionId}"`,
1098
- severity: "recoverable"
1099
- });
1100
- continue;
1101
- }
1102
- if (caption.speech_part_id !== partId) issues.push({
1103
- code: "invalid_speech_caption_reference",
1104
- path: `/part_library/${captionId}/caption/speech_part_id`,
1105
- message: `Caption "${captionId}" does not point back to speech "${partId}"`
1106
- });
1107
- }
1108
- if (part.caption != null) {
1109
- const speechId = part.caption.speech_part_id;
1110
- if (speechId != null && partLibrary[speechId]?.speech == null) issues.push({
1111
- code: "invalid_speech_caption_reference",
1112
- path: `/part_library/${partId}/caption/speech_part_id`,
1113
- message: `Caption "${partId}" references missing speech "${speechId}"`,
1114
- severity: "recoverable"
1115
- });
1116
- }
1117
- }
1118
- }
1119
- function getPartUnionKind(part) {
1120
- if (part.video_clip != null) return "video_clip";
1121
- if (part.speech != null) return "speech";
1122
- if (part.caption != null) return "caption";
1123
- if (part.bgm != null) return "bgm";
1124
- }
1125
- function getPartKind(part) {
1126
- return part.video_clip?.kind ?? part.speech?.kind ?? part.caption?.kind ?? part.bgm?.kind;
1127
- }
1128
- function zodPath(path) {
1129
- if (path.length === 0) return "/";
1130
- return `/${path.map(String).join("/")}`;
1131
- }
1132
- //#endregion
1133
- //#region src/document/projection.ts
1134
- /**
1135
- * Projection between the authoritative `VideoDocument` and the legacy
1136
- * `VideoDraft` read-view (RFC 02 §5/§7). Both directions live here:
1137
- *
1138
- * - `toVideoDocument` ingests a `VideoDraft`, deriving each item's `position`
1139
- * from the legacy absolute layout + aggregations; derived values (abs time,
1140
- * `part_aggregations`, total duration) are dropped.
1141
- * - `fromVideoDocument` solves a `VideoDocument` back into a `VideoDraft` via the
1142
- * timeline-core cascade, re-deriving exactly those values.
1143
- *
1144
- * Business validation lives in `validation.ts`; `fromVideoDocument` asserts a
1145
- * valid document before solving.
1146
- */
1147
- /**
1148
- * Ingest the legacy `VideoDraft` into the authoritative `VideoDocument`,
1149
- * deriving each item's `time_position` from the legacy absolute layout +
1150
- * aggregations (RFC 02 §4/§5). Absolute time, `part_aggregations`, and total
1151
- * duration are dropped — they are re-derived by the projection.
1152
- */
1153
- function toVideoDocument(draft) {
1154
- const speechHost = buildSpeechHostMap(draft.part_aggregations);
1155
- const partLibrary = draft.part_library ?? {};
1156
- const toTrack = (track, isMain) => {
1157
- if (track == null) return void 0;
1158
- return {
1159
- id: track.id,
1160
- parts_kind: track.parts_kind,
1161
- is_hidden: track.is_hidden,
1162
- items: (track.items ?? []).map((item) => deriveItem(item, isMain, speechHost, partLibrary))
1163
- };
1164
- };
1165
- const mainTrack = toTrack(draft.main_track, true);
1166
- const tracks = [
1167
- ...(draft.above_main_tracks ?? []).map((t) => toTrack(t, false)),
1168
- ...mainTrack == null ? [] : [mainTrack],
1169
- ...(draft.below_main_tracks ?? []).map((t) => toTrack(t, false))
1170
- ];
1171
- return {
1172
- meta: {
1173
- schema_version: VIDEO_DOCUMENT_SCHEMA_VERSION,
1174
- draft_id: draft.id,
1175
- project_id: draft.project_id,
1176
- owner_id: draft.owner_id,
1177
- thumbnail_storage_key: draft.thumbnail_storage_key,
1178
- chat_session_id: draft.chat_session_id,
1179
- video_creation_settings: clone(draft.video_creation_settings),
1180
- version: draft.version
1181
- },
1182
- timeline: draft.timeline == null ? void 0 : { unit_time_ms: draft.timeline.unit_time_ms },
1183
- tracks,
1184
- part_library: toAuthoritativePartLibrary(draft.part_library)
1185
- };
1186
- }
1187
- /**
1188
- * Map the legacy `VideoDraft` part library into the authoritative shape
1189
- * (reference/17 §5): the authoritative parts store no derived part-level
1190
- * duration, so the read-view `duration_ms` is dropped from video / speech / bgm.
1191
- * For a caption the legacy `duration_ms` is the generation-time length, stored
1192
- * authoritatively as `initial_duration_ms`.
1193
- */
1194
- function toAuthoritativePartLibrary(partLibrary) {
1195
- if (partLibrary == null) return void 0;
1196
- const out = {};
1197
- for (const [partId, part] of Object.entries(partLibrary)) {
1198
- if (typeof part !== "object" || part == null) continue;
1199
- if (part.video_clip != null) out[partId] = { video_clip: withTrimWindowFromDuration(part.video_clip) };
1200
- else if (part.speech != null) {
1201
- const { duration_ms, rest } = splitDurationMs(part.speech);
1202
- out[partId] = { speech: {
1203
- ...rest,
1204
- media_duration_ms: rest.media_duration_ms ?? duration_ms
1205
- } };
1206
- } else if (part.caption != null) {
1207
- const { duration_ms, rest } = splitDurationMs(part.caption);
1208
- out[partId] = { caption: {
1209
- ...rest,
1210
- initial_duration_ms: rest.initial_duration_ms ?? duration_ms
1211
- } };
1212
- } else if (part.bgm != null) out[partId] = { bgm: omitDurationMs(part.bgm) };
1213
- }
1214
- return out;
1215
- }
1216
- /**
1217
- * Build a speech-host map from a part_aggregations list. Used by
1218
- * `toVideoDocument` (legacy VideoDraft → VideoDocument) to recover each speech's
1219
- * host video clip and relative offset. Aggregation items with null ids are
1220
- * skipped (malformed input tolerance).
1221
- */
1222
- function buildSpeechHostMap(aggregations) {
1223
- const map = /* @__PURE__ */ new Map();
1224
- for (const aggregation of aggregations ?? []) {
1225
- const host = aggregation.body_part_id;
1226
- if (host == null) continue;
1227
- for (const attachment of aggregation.attachments ?? []) {
1228
- if (attachment.part_id == null) continue;
1229
- map.set(attachment.part_id, {
1230
- hostPartId: host,
1231
- offsetMs: Math.round(attachment.relative_time_position ?? 0)
1232
- });
1233
- }
1234
- }
1235
- return map;
1236
- }
1237
- /**
1238
- * Derive `time_position` (and `fallbackAbsMs` for anchored items) from a legacy
1239
- * absolute time + pre-built speech-host map + part library. The three-branch
1240
- * rule (RFC 02 §4/§5, reference/17 §3):
1241
- *
1242
- * 1. main-track → `sequential`
1243
- * 2. speech/attachment (part_id in speechHost) → `anchored(host, offsetMs)`
1244
- * 3. caption (part has `speech_part_id`) → `anchored(speech, start_ms)`
1245
- * 4. everything else → `absolute(abs)`
1246
- *
1247
- * `partLibrary` values may be `PartUnion | string | undefined` (draft raw
1248
- * form); only object-typed entries are inspected for `caption`.
1249
- */
1250
- function derivePositionFromAbs(partId, abs, isMain, speechHost, partLibrary) {
1251
- if (isMain) return { timePosition: { mode: "sequential" } };
1252
- const host = speechHost.get(partId);
1253
- if (host != null) return {
1254
- timePosition: {
1255
- mode: "anchored",
1256
- anchorPartId: host.hostPartId,
1257
- offsetMs: host.offsetMs
1258
- },
1259
- fallbackAbsMs: abs
1260
- };
1261
- const part = partLibrary[partId];
1262
- const captionPart = typeof part === "object" && part != null ? part.caption : void 0;
1263
- const speechId = captionPart?.speech_part_id ?? void 0;
1264
- if (speechId != null) return {
1265
- timePosition: {
1266
- mode: "anchored",
1267
- anchorPartId: speechId,
1268
- offsetMs: Math.round(captionPart?.start_ms ?? 0)
1269
- },
1270
- fallbackAbsMs: abs
1271
- };
1272
- return { timePosition: {
1273
- mode: "absolute",
1274
- offsetMs: abs
1275
- } };
1276
- }
1277
- /** Derive one item's authoritative `time_position` from its legacy abs + aggregation. */
1278
- function deriveItem(item, isMain, speechHost, partLibrary) {
1279
- const partId = item.part_id ?? "";
1280
- const { timePosition, fallbackAbsMs } = derivePositionFromAbs(partId, Math.round(item.abs_time_position ?? 0), isMain, speechHost, partLibrary);
1281
- return fallbackAbsMs == null ? {
1282
- part_id: partId,
1283
- time_position: timePosition
1284
- } : {
1285
- part_id: partId,
1286
- time_position: timePosition,
1287
- fallback_abs_ms: fallbackAbsMs
1288
- };
1289
- }
1290
- /**
1291
- * Project the authoritative `VideoDocument` back into the legacy `VideoDraft`
1292
- * read-view, solving each item's absolute position, the `part_aggregations`, and
1293
- * the total duration via the timeline-core cascade.
1294
- *
1295
- * The read-view is a compatibility contract, and the IDL declares
1296
- * `Timeline.unit_time_ms` and `Track.is_hidden` `@required`
1297
- * (`video_draft_comp.smithy`) — so a projection that omitted them handed every
1298
- * consumer a payload that violated the shape it claims to satisfy, leaving each
1299
- * one to invent its own default. Two already had, and they disagreed: the agent
1300
- * harness used `unit_time_ms: 0`, director used a frame at 30fps. Defaulting here
1301
- * makes the contract true at the one place that produces it.
1302
- *
1303
- * These two are defaultable because the projection knows their values on its own:
1304
- * absent `unit_time_ms` means "no display granularity was ever stated" and absent
1305
- * `is_hidden` means "this track was never hidden". `version` is NOT defaultable
1306
- * here — its only honest value is server state (`update_seq`) that a
1307
- * `VideoDocument` cannot see, so it stays absent and the HTTP layer fills it.
1308
- */
1309
- function fromVideoDocument(document) {
1310
- assertValidVideoDocument(document);
1311
- const view = solveVideoDocument(document);
1312
- const tracks = document.tracks ?? [];
1313
- const mainTrack = tracks.find((t) => t.parts_kind === "video_clip");
1314
- const aboveTracks = tracks.filter((t) => t.parts_kind === "caption");
1315
- const belowTracks = tracks.filter((t) => t.parts_kind === "speech" || t.parts_kind === "bgm");
1316
- return {
1317
- id: document.meta.draft_id,
1318
- project_id: document.meta.project_id,
1319
- owner_id: document.meta.owner_id,
1320
- thumbnail_storage_key: document.meta.thumbnail_storage_key,
1321
- timeline: {
1322
- duration_ms: view.durationMs,
1323
- unit_time_ms: document.timeline?.unit_time_ms ?? 33.333
1324
- },
1325
- video_creation_settings: clone(document.meta.video_creation_settings),
1326
- chat_session_id: document.meta.chat_session_id,
1327
- main_track: draftTrack(mainTrack, view.absByPartId),
1328
- above_main_tracks: aboveTracks.map((t) => draftTrack(t, view.absByPartId)),
1329
- below_main_tracks: belowTracks.map((t) => draftTrack(t, view.absByPartId)),
1330
- part_aggregations: view.aggregations,
1331
- part_library: toReadViewPartLibrary(view.partLibrary, view.durationMs, view.derivedFillerPartIds, captionDurationOverrides(document)),
1332
- version: document.meta.version
1333
- };
1334
- }
1335
- /**
1336
- * Map the authoritative part library into the `VideoDraft` read-view shape,
1337
- * re-injecting the derived effective `duration_ms` downstream expects
1338
- * (reference/17 §5/§7) — the authoritative parts store no part-level duration:
1339
- *
1340
- * - video clip → `(play_out - play_in) / speed` (`effectiveVideoClipDurationMs`)
1341
- * - speech → its intrinsic `media_duration_ms` (no trim/speed)
1342
- * - caption → its generation-time `initial_duration_ms`
1343
- * - bgm → the timeline total (`durationMs`)
1344
- *
1345
- * Parts are deep-cloned; the engine extensions (`media_duration_ms`,
1346
- * `initial_duration_ms`) are kept alongside the injected `duration_ms`.
1347
- *
1348
- * `derivedFillerPartIds` are skipped: the solve minted them to lay the track out
1349
- * and they are not part of the document (see `fromVideoDocument`). An empty clip a
1350
- * writer placed deliberately is NOT in that set and is projected normally.
1351
- */
1352
- function toReadViewPartLibrary(partLibrary, durationMs, derivedFillerPartIds, captionDurationByPartId) {
1353
- const out = {};
1354
- for (const [partId, part] of Object.entries(partLibrary)) {
1355
- if (derivedFillerPartIds.has(partId)) continue;
1356
- if (part.video_clip != null) {
1357
- const clip = clone(part.video_clip);
1358
- out[partId] = { video_clip: {
1359
- ...clip,
1360
- duration_ms: effectiveVideoClipDurationMs(clip)
1361
- } };
1362
- } else if (part.speech != null) {
1363
- const speech = clone(part.speech);
1364
- out[partId] = { speech: {
1365
- ...speech,
1366
- duration_ms: speech.media_duration_ms
1367
- } };
1368
- } else if (part.caption != null) {
1369
- const caption = clone(part.caption);
1370
- out[partId] = { caption: {
1371
- ...caption,
1372
- duration_ms: captionDurationByPartId.get(partId) ?? caption.initial_duration_ms
1373
- } };
1374
- } else if (part.bgm != null) out[partId] = { bgm: {
1375
- ...clone(part.bgm),
1376
- duration_ms: durationMs
1377
- } };
1378
- }
1379
- return out;
1380
- }
1381
- function captionDurationOverrides(document) {
1382
- const result = /* @__PURE__ */ new Map();
1383
- for (const track of document.tracks ?? []) {
1384
- if (track.parts_kind !== "caption") continue;
1385
- for (const item of track.items ?? []) if (item.duration_override_ms != null) result.set(item.part_id, item.duration_override_ms);
1386
- }
1387
- return result;
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(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(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(value) {
1435
- if (Array.isArray(value)) return value.map((item) => clone(item));
1436
- if (value != null && typeof value === "object") {
1437
- const result = {};
1438
- for (const [key, child] of Object.entries(value)) result[key] = clone(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 src/document/mirror-read.ts
1496
- /**
1497
- * Project the mirror state (`VideoDocumentDraft`) into the authoritative
1498
- * `VideoDocument`. The storage shape is isomorphic to the domain shape (RFC 03
1499
- * §4, reference/17 §4: meta map + a single `tracks` list + part_library), so this
1500
- * is a near-identity — it reads `time_position` / `fallback_abs_ms` JSON blobs
1501
- * back into structured values and trims empty strings, nothing more.
1502
- *
1503
- * It maps only authoritative facts (RFC 02 §6): `part_id` + `time_position`. The
1504
- * projection-derived `VideoDraft` read-view (absolute time, `part_aggregations`,
1505
- * total duration) is solved separately by the cascade, not here.
1506
- *
1507
- * It reads in-memory mirror state (O(n) over the document), not a Loro
1508
- * `toJSON()` FFI rebuild — the cost the prior schema adapter paid on every
1509
- * `snapshot()`.
1510
- */
1511
- function readVideoDocumentFromDraft(draft) {
1512
- const partLibrary = {};
1513
- for (const [partId, payload] of recordEntries(draft.part_library)) {
1514
- const part = draftToPartUnion(payload);
1515
- if (part != null) partLibrary[partId] = part;
1516
- }
1517
- const timeline = draft.timeline;
1518
- const hasTimeline = timeline?.unit_time_ms != null;
1519
- const meta = draft.meta;
1520
- return {
1521
- meta: {
1522
- schema_version: VIDEO_DOCUMENT_SCHEMA_VERSION,
1523
- draft_id: emptyToUndefined(meta?.draft_id),
1524
- project_id: emptyToUndefined(meta?.project_id),
1525
- owner_id: emptyToUndefined(meta?.owner_id),
1526
- thumbnail_storage_key: emptyToUndefined(meta?.thumbnail_storage_key),
1527
- chat_session_id: emptyToUndefined(meta?.chat_session_id),
1528
- video_creation_settings: meta?.video_creation_settings ?? void 0,
1529
- version: meta?.version ?? void 0
1530
- },
1531
- timeline: hasTimeline ? { unit_time_ms: timeline.unit_time_ms } : void 0,
1532
- tracks: rowsToTracks(draft.tracks),
1533
- part_library: partLibrary
1534
- };
1535
- }
1536
- /** Map a movable-list of track rows (`$cid`-bearing) into domain tracks, dropping empty-id placeholders. */
1537
- function rowsToTracks(rows) {
1538
- return (rows ?? []).filter((row) => row.id != null && row.id !== "").map(rowToTrack);
1539
- }
1540
- function rowToTrack(row) {
1541
- return {
1542
- id: row.id,
1543
- parts_kind: row.parts_kind ?? void 0,
1544
- is_hidden: row.is_hidden ?? void 0,
1545
- items: (row.items ?? []).map((item) => {
1546
- const result = {
1547
- part_id: item.part_id ?? "",
1548
- time_position: item.time_position
1549
- };
1550
- if (item.fallback_abs_ms != null) result.fallback_abs_ms = item.fallback_abs_ms;
1551
- if (item.duration_override_ms != null) result.duration_override_ms = item.duration_override_ms;
1552
- return result;
1553
- })
1554
- };
1555
- }
1556
- function emptyToUndefined(value) {
1557
- return value == null || value === "" ? void 0 : value;
1558
- }
1559
- //#endregion
1560
- //#region src/document/mirror-adapter.ts
1561
- /**
1562
- * Storage-layer adapter that backs a `VideoDocument` with `loro-mirror` (ADR
1563
- * 0008). The mirror holds an in-memory immutable state synced to the `LoroDoc`
1564
- * by declarative diff, replacing the hand-rolled `@mengine/schema` adapter:
1565
- *
1566
- * - `snapshot()` projects the mirror's in-memory state to the read model — no
1567
- * per-call `toJSON()` FFI rebuild.
1568
- * - `transact(edit, audit)` runs the whole op in one `mirror.setState` callback:
1569
- * one diff, one `doc.commit` carrying the audit message. The callback edits an
1570
- * immer draft, so a throw inside it discards the draft and never touches Loro
1571
- * (natural rollback) — no `guard` / `rollback` / `openTransaction` machinery.
1572
- * - mirror's `idSelector` (track items keyed by `part_id`) diffs reorders to
1573
- * real Loro `move` ops on every lane, so per-item CRDT identity survives on
1574
- * main and secondary tracks alike.
1575
- */
1576
- var MirrorVideoDocumentAdapter = class {
1577
- doc;
1578
- mirror;
1579
- constructor(doc) {
1580
- this.doc = doc;
1581
- this.mirror = new Mirror({
1582
- doc,
1583
- schema: videoDocumentMirrorSchema
1584
- });
1585
- }
1586
- snapshot() {
1587
- return readVideoDocumentFromDraft(this.mirror.getState());
1588
- }
1589
- /**
1590
- * True once the doc holds real document content. A fresh mirror over an empty
1591
- * doc still reports defaulted root maps, so probe the stored `schema_version`
1592
- * (empty until a snapshot is bootstrapped or synced in).
1593
- */
1594
- hasContent() {
1595
- return (this.mirror.getState().meta?.schema_version ?? "") !== "";
1596
- }
1597
- /**
1598
- * Apply one op as a single transaction. `edit` mutates the immer draft; mirror
1599
- * diffs the result and commits once with the audit `message`. A throw in `edit`
1600
- * discards the draft (Loro untouched). When `edit` produces no change, mirror
1601
- * skips the commit — matching the prior "empty op leaves no audit" behavior.
1602
- */
1603
- transact(edit, audit) {
1604
- this.mirror.setState((draft) => {
1605
- edit(draft);
1606
- }, {
1607
- origin: "mengine.semantic_editor",
1608
- message: JSON.stringify({
1609
- semantic_op: audit.kind,
1610
- payload: audit.payload,
1611
- intent: audit.intent ?? null,
1612
- actor: audit.actor ?? null
1613
- })
1614
- });
1615
- }
1616
- };
1617
- /** Build a fresh Loro doc seeded with `document` through the mirror. */
1618
- function createMirrorVideoDocument(document, options = {}) {
1619
- assertValidVideoDocument(document);
1620
- const doc = new LoroDoc();
1621
- if (options.peerId != null) doc.setPeerId(options.peerId);
1622
- new Mirror({
1623
- doc,
1624
- schema: videoDocumentMirrorSchema
1625
- }).setState((draft) => {
1626
- seedDraft(draft, document);
1627
- }, { origin: options.origin ?? "mengine.bootstrap" });
1628
- return doc;
1629
- }
1630
- /** Build a `MirrorVideoDocumentAdapter` over a fresh doc seeded with `document`. */
1631
- function createMirrorVideoDocumentAdapter(document, options) {
1632
- return new MirrorVideoDocumentAdapter(createMirrorVideoDocument(document, options));
1633
- }
1634
- /** Write a whole `VideoDocument` into the mirror draft (used to seed a fresh doc). */
1635
- function seedDraft(draft, document) {
1636
- draft.meta = {
1637
- schema_version: document.meta.schema_version,
1638
- draft_id: document.meta.draft_id ?? void 0,
1639
- project_id: document.meta.project_id ?? void 0,
1640
- owner_id: document.meta.owner_id ?? void 0,
1641
- thumbnail_storage_key: document.meta.thumbnail_storage_key ?? void 0,
1642
- chat_session_id: document.meta.chat_session_id ?? void 0,
1643
- video_creation_settings: document.meta.video_creation_settings ?? void 0,
1644
- version: document.meta.version ?? void 0
1645
- };
1646
- draft.timeline = { unit_time_ms: document.timeline?.unit_time_ms ?? void 0 };
1647
- draft.tracks = (document.tracks ?? []).map(toTrackRow);
1648
- draft.part_library = {};
1649
- for (const [partId, part] of Object.entries(document.part_library ?? {})) draft.part_library[partId] = partUnionToDraft(part);
1650
- }
1651
- /** Map a domain `Track` to a draft track row (no lane/lane_order — see schema). */
1652
- function toTrackRow(track) {
1653
- return {
1654
- id: track.id ?? "",
1655
- parts_kind: track.parts_kind ?? void 0,
1656
- is_hidden: track.is_hidden ?? void 0,
1657
- items: (track.items ?? []).map((item) => ({
1658
- part_id: item.part_id,
1659
- time_position: item.time_position,
1660
- fallback_abs_ms: item.fallback_abs_ms,
1661
- duration_override_ms: item.duration_override_ms
1662
- }))
1663
- };
1664
- }
1665
- //#endregion
1666
- export { partDurationMs as A, reassignSpeechesToVideoClipsByTime as C, syncAggregatedClipsTimePosition as D, resolveSpeechOverlapByShiftingVideos as E, partUnionToDraft as F, recordEntries as I, videoDocumentMirrorSchema as L, DEFAULT_UNIT_TIME_MS as M, VIDEO_DOCUMENT_SCHEMA_VERSION as N, TIMELINE_SKELETON_DURATION_MS as O, effectiveVideoClipDurationMs as P, base64ToBytes as R, fillMainTrackTimeGaps as S, resolveAllSpeechOverlaps as T, findLaneTrack as _, buildInitialVideoDocument as a, cascadeAfterVideoClipChanges as b, fromVideoDocument as c, assertValidVideoDocument as d, validateVideoDocument as f, ensureLaneTrack as g, LANE_KINDS_IN_STACK_ORDER as h, readVideoDocumentFromDraft as i, safeDurationMs as j, isEmptyVideoClip as k, toVideoDocument as l, videoDocumentSchema as m, createMirrorVideoDocument as n, buildSpeechHostMap as o, partUnionSchema as p, createMirrorVideoDocumentAdapter as r, derivePositionFromAbs as s, MirrorVideoDocumentAdapter as t, VideoDocumentValidationError as u, laneTrackId as v, recalculateTimelineDuration as w, arrangeMainTrackSeamlessly as x, solveVideoDocument as y, bytesToBase64 as z };