@mengine/medeo-client 0.1.3 → 1.0.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/document-DgffKwRw.js +1593 -0
- package/dist/index-6e5cbdM3.d.ts +475 -0
- package/dist/index-DRUGsbm2.d.ts +1421 -0
- package/dist/index.d.ts +28 -1813
- package/dist/index.js +133 -1901
- package/dist/schemas.d.ts +2 -0
- package/dist/schemas.js +378 -0
- package/dist/testing.d.ts +46 -0
- package/dist/testing.js +339 -0
- package/package.json +10 -5
package/dist/index.js
CHANGED
|
@@ -1,24 +1,17 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import {
|
|
3
|
-
import { z } from "zod";
|
|
4
|
-
import { LoroDoc } from "loro-crdt";
|
|
1
|
+
import { A as isEmptyVideoClip, B as bytesToBase64, C as fillMainTrackTimeGaps, D as resolveSpeechOverlapByShiftingVideos, E as resolveAllSpeechOverlaps, F as speedOf, I as partUnionToDraft, L as recordEntries, M as safeDurationMs, N as VIDEO_DOCUMENT_SCHEMA_VERSION, O as syncAggregatedClipsTimePosition, P as effectiveVideoClipDurationMs, R as videoDocumentMirrorSchema, S as arrangeMainTrackSeamlessly, T as recalculateTimelineDuration, _ as videoDocumentSchema, a as createMirrorVideoDocument, b as solveVideoDocument, c as readVideoDocumentFromDraft, d as fromVideoDocument, f as toVideoDocument, g as partUnionSchema, h as validateVideoDocument, i as MirrorVideoDocumentAdapter, j as partDurationMs, k as TIMELINE_SKELETON_DURATION_MS, l as buildSpeechHostMap, m as assertValidVideoDocument, n as createPlainMemoryAdapter, o as createMirrorVideoDocumentAdapter, p as VideoDocumentValidationError, r as generatePartId, s as writeVideoDocumentToDraft, t as PlainMemoryAdapter, u as derivePositionFromAbs, v as ensureLaneTrack, w as reassignSpeechesToVideoClipsByTime, x as cascadeAfterVideoClipChanges, y as findLaneTrack, z as base64ToBytes } from "./document-DgffKwRw.js";
|
|
2
|
+
import { addSpeechesInputSchema, addVideoClipsInputSchema, adjustBgmVolumeInputSchema, adjustSpeechVolumeInputSchema, adjustVideoClipDurationInputSchema, adjustVideoClipVolumeInputSchema, changeSpeechScriptInputSchema, changeSpeechVoiceInputSchema, deleteBgmInputSchema, deleteSpeechesInputSchema, deleteVideoClipsInputSchema, moveSpeechesInputSchema, moveVideoClipsInputSchema, replaceVideoClipContentInputSchema, setBgmInputSchema, setCaptionStyleInputSchema, setCaptionVisibilityInputSchema, setVideoClipSpeedShiftInputSchema, t as schemas_exports } from "./schemas.js";
|
|
5
3
|
import { ClientServerSynchronizer, DocManager } from "@mengine/sync";
|
|
6
4
|
import { DisposableSet, EventBus, Task } from "@mengine/utils";
|
|
7
5
|
import { BaseDocStorage, DummyConnection } from "@mengine/storage";
|
|
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
6
|
//#region src/client/http-client.ts
|
|
7
|
+
/**
|
|
8
|
+
* Public route prefix of the mengine doc-collaboration API. Must stay in lockstep
|
|
9
|
+
* with `apps/mengine-server`'s `API_PREFIX` — the two form the private wire
|
|
10
|
+
* contract between this client and the relay server. Versioned, mengine-owned
|
|
11
|
+
* namespace (mirrors the cluster's `/oapi/v1` precedent for an independent
|
|
12
|
+
* subsystem) rather than folding into the shared `/api/v2/*` space.
|
|
13
|
+
*/
|
|
14
|
+
const MENGINE_API_PREFIX = "/api/mengine/v1";
|
|
22
15
|
var MengineHttpRequestError = class extends Error {
|
|
23
16
|
status;
|
|
24
17
|
payload;
|
|
@@ -99,7 +92,7 @@ var MengineHttpClient = class {
|
|
|
99
92
|
return payload;
|
|
100
93
|
}
|
|
101
94
|
endpoint(path) {
|
|
102
|
-
return `${this.options.httpOrigin.replace(/\/$/, "")}/
|
|
95
|
+
return `${this.options.httpOrigin.replace(/\/$/, "")}${MENGINE_API_PREFIX}/docs/${encodeURIComponent(this.options.docId)}/${path}`;
|
|
103
96
|
}
|
|
104
97
|
};
|
|
105
98
|
async function safeReadJson(response) {
|
|
@@ -163,1886 +156,6 @@ function findSseFrameBoundary(buffer) {
|
|
|
163
156
|
return Math.min(lf, crlf);
|
|
164
157
|
}
|
|
165
158
|
//#endregion
|
|
166
|
-
//#region src/document/mirror-schema.ts
|
|
167
|
-
/**
|
|
168
|
-
* Declarative `loro-mirror` schema for `VideoDocument` — the canonical structural
|
|
169
|
-
* SSOT and live storage wire (replaces the hand-rolled `@mengine/schema`
|
|
170
|
-
* definition + adapter, see ADR 0008).
|
|
171
|
-
*
|
|
172
|
-
* Mirror gives an in-memory immutable state synced to the Loro doc by declarative
|
|
173
|
-
* diff, so the storage layer no longer hand-writes per-region reconcile or a
|
|
174
|
-
* transaction state machine. The shapes here store only authoritative facts
|
|
175
|
-
* (RFC 02 §6): per-item absolute time, `part_aggregations`, and total duration
|
|
176
|
-
* are projection-derived (cascade-solved at read time), so they are absent here.
|
|
177
|
-
*
|
|
178
|
-
* - tracks live in a single `tracks` `LoroMovableList` keyed by track id
|
|
179
|
-
* (reference/17 §4). Lane is expressed by each track's `parts_kind`, not by
|
|
180
|
-
* which container it lives in; order is the movable-list order itself. There is
|
|
181
|
-
* no `lane` / `lane_order` field, so two peers inserting tracks never collide
|
|
182
|
-
* on an order integer — list moves are conflict-free (RFC 03 §4). The legacy
|
|
183
|
-
* keyed-map (`LoroMapRecord` + an integer `lane_order`) and the prior three
|
|
184
|
-
* named containers are gone; the `main` / `above` / `below` three-pane view is
|
|
185
|
-
* rebuilt by the projection from `parts_kind`.
|
|
186
|
-
* - each track's `items` is a `LoroMovableList` keyed by `part_id`: a part is
|
|
187
|
-
* placed at most once per lane, so `part_id` is the stable placement identity.
|
|
188
|
-
* Reorder diffs to a real Loro `move`, preserving per-item CRDT identity on
|
|
189
|
-
* every lane (main and secondary alike).
|
|
190
|
-
* - `time_position` is the authoritative positioning fact (RFC 02 §4,
|
|
191
|
-
* reference/17 §3), carried as an opaque JSON blob string (a whole tagged-union
|
|
192
|
-
* value, last-writer-wins). The derived `fallback_abs_ms` snapshot sits beside
|
|
193
|
-
* it as its own number field — a separate LWW unit so a projection refresh
|
|
194
|
-
* never clobbers a `time_position` edit.
|
|
195
|
-
* - meta lives in a `meta` LoroMap: the mirror root only accepts container
|
|
196
|
-
* schemas, so meta scalars can't sit at the root as bare values. The domain
|
|
197
|
-
* `VideoDocument` keeps the same `meta` wrapper, so the shapes are isomorphic
|
|
198
|
-
* and the read/seed mapping is a near-identity.
|
|
199
|
-
* - `part_library` values are nested `LoroMap`s: the value is a `LoroMap` with
|
|
200
|
-
* four mutually-exclusive optional part sub-maps (`video_clip` / `speech` /
|
|
201
|
-
* `caption` / `bgm`), each a `LoroMap` whose fields are stored as real Loro
|
|
202
|
-
* sub-keys. This makes each field its own CRDT unit, so two peers editing
|
|
203
|
-
* different fields of the same part (e.g. one volume, one play_out) merge
|
|
204
|
-
* field-by-field instead of one whole-value overwrite. Field `required` mirrors
|
|
205
|
-
* the Smithy `@required` contract (see `video_draft_comp.smithy`). `kind` is not
|
|
206
|
-
* stored (the present sub-key names the kind); `duration_ms` is not stored (it
|
|
207
|
-
* is derived, reference/17 §5). Nested values that the engine does not edit
|
|
208
|
-
* field-by-field — `speed_shift` (a tagged union), `voice`, `caption_ids` — stay
|
|
209
|
-
* as opaque JSON-blob sub-keys via `transform`. The four sub-keys are optional,
|
|
210
|
-
* so the "exactly one part kind" union invariant is not enforced by the schema
|
|
211
|
-
* type; it is rebuilt by `draftToPartUnion` on read and gated by zod on write.
|
|
212
|
-
* - a caption's `style` is the one lazily-created optional child container, so
|
|
213
|
-
* `captionPart` sets `mergeableMapChildContainers: true` — two peers can
|
|
214
|
-
* concurrently first-create the same caption's `style` (one attribute each), and
|
|
215
|
-
* only a mergeable child converges instead of last-writer-wins dropping one. All
|
|
216
|
-
* other nested maps are keyed by a unique id (`part_library`) or created
|
|
217
|
-
* atomically with their parent (`partValue`'s part sub-map), so they never hit
|
|
218
|
-
* that concurrent-first-create fork and stay on plain `setContainer`. See the
|
|
219
|
-
* note on `captionPart` below.
|
|
220
|
-
* - `video_creation_settings` is an opaque JSON blob carried in a string via
|
|
221
|
-
* `transform`: a whole value, last-writer-wins (no field-level concurrent edits).
|
|
222
|
-
*/
|
|
223
|
-
/**
|
|
224
|
-
* JSON-blob transform for an opaque, last-writer-wins value carried in a Loro
|
|
225
|
-
* string. The field must be declared `required: false`: an absent field decodes
|
|
226
|
-
* to `undefined` (mirror never calls `decode`/`encode` for null/undefined),
|
|
227
|
-
* which is how we represent "no value" instead of encoding a `null` sentinel.
|
|
228
|
-
*/
|
|
229
|
-
function jsonTransform() {
|
|
230
|
-
return {
|
|
231
|
-
decode: (value) => JSON.parse(value),
|
|
232
|
-
encode: (value) => JSON.stringify(value),
|
|
233
|
-
isEqual: "encoded-value-equality"
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
const trackItem = schema.LoroMap({
|
|
237
|
-
part_id: schema.String(),
|
|
238
|
-
time_position: schema.String().transform(jsonTransform()),
|
|
239
|
-
fallback_abs_ms: schema.Number({ required: false })
|
|
240
|
-
});
|
|
241
|
-
const track = schema.LoroMap({
|
|
242
|
-
id: schema.String(),
|
|
243
|
-
parts_kind: schema.String({ required: false }),
|
|
244
|
-
is_hidden: schema.Boolean({ required: false }),
|
|
245
|
-
items: schema.LoroMovableList(trackItem, (item) => item.part_id)
|
|
246
|
-
});
|
|
247
|
-
const videoClipPart = schema.LoroMap({
|
|
248
|
-
id: schema.String(),
|
|
249
|
-
play_in: schema.Number(),
|
|
250
|
-
play_out: schema.Number(),
|
|
251
|
-
volume: schema.Number(),
|
|
252
|
-
origin_media_id: schema.String(),
|
|
253
|
-
speed_shift: schema.String({ required: false }).transform(jsonTransform())
|
|
254
|
-
}, { required: false });
|
|
255
|
-
const speechPart = schema.LoroMap({
|
|
256
|
-
id: schema.String(),
|
|
257
|
-
media_duration_ms: schema.Number(),
|
|
258
|
-
audio_script: schema.String(),
|
|
259
|
-
volume: schema.Number(),
|
|
260
|
-
audio_storage_key: schema.String(),
|
|
261
|
-
origin_speech_id: schema.String(),
|
|
262
|
-
voice: schema.String().transform(jsonTransform()),
|
|
263
|
-
caption_ids: schema.String().transform(jsonTransform())
|
|
264
|
-
}, { required: false });
|
|
265
|
-
const captionStyle = schema.LoroMap({
|
|
266
|
-
font_id: schema.String({ required: false }),
|
|
267
|
-
font_size: schema.Number({ required: false }),
|
|
268
|
-
font_color: schema.String({ required: false }),
|
|
269
|
-
font_weight: schema.Number({ required: false }),
|
|
270
|
-
entrance_animation: schema.String({ required: false }),
|
|
271
|
-
entrance_animation_duration_ms: schema.Number({ required: false }),
|
|
272
|
-
stroke_color: schema.String({ required: false }),
|
|
273
|
-
stroke_width: schema.Number({ required: false }),
|
|
274
|
-
position_x: schema.Number({ required: false }),
|
|
275
|
-
position_y: schema.Number({ required: false })
|
|
276
|
-
}, { required: false });
|
|
277
|
-
const captionPart = schema.LoroMap({
|
|
278
|
-
id: schema.String(),
|
|
279
|
-
initial_duration_ms: schema.Number(),
|
|
280
|
-
speech_part_id: schema.String(),
|
|
281
|
-
text: schema.String(),
|
|
282
|
-
start_ms: schema.Number(),
|
|
283
|
-
style: captionStyle
|
|
284
|
-
}, {
|
|
285
|
-
required: false,
|
|
286
|
-
mergeableMapChildContainers: true
|
|
287
|
-
});
|
|
288
|
-
const bgmPart = schema.LoroMap({
|
|
289
|
-
id: schema.String(),
|
|
290
|
-
audio_storage_key: schema.String(),
|
|
291
|
-
volume: schema.Number(),
|
|
292
|
-
origin_media_id: schema.String()
|
|
293
|
-
}, { required: false });
|
|
294
|
-
const partValue = schema.LoroMap({
|
|
295
|
-
video_clip: videoClipPart,
|
|
296
|
-
speech: speechPart,
|
|
297
|
-
caption: captionPart,
|
|
298
|
-
bgm: bgmPart
|
|
299
|
-
});
|
|
300
|
-
const videoDocumentMirrorSchema = schema({
|
|
301
|
-
meta: schema.LoroMap({
|
|
302
|
-
schema_version: schema.String({ required: false }),
|
|
303
|
-
draft_id: schema.String({ required: false }),
|
|
304
|
-
project_id: schema.String({ required: false }),
|
|
305
|
-
owner_id: schema.String({ required: false }),
|
|
306
|
-
thumbnail_storage_key: schema.String({ required: false }),
|
|
307
|
-
chat_session_id: schema.String({ required: false }),
|
|
308
|
-
video_creation_settings: schema.String({ required: false }).transform(jsonTransform()),
|
|
309
|
-
version: schema.Number({ required: false })
|
|
310
|
-
}),
|
|
311
|
-
timeline: schema.LoroMap({ unit_time_ms: schema.Number({ required: false }) }),
|
|
312
|
-
tracks: schema.LoroMovableList(track, (t) => t.id),
|
|
313
|
-
part_library: schema.LoroMapRecord(partValue)
|
|
314
|
-
});
|
|
315
|
-
/** Project a discriminated `PartUnion` into the draft's four-optional shape. */
|
|
316
|
-
function partUnionToDraft(part) {
|
|
317
|
-
if (part.video_clip != null) return { video_clip: omitKind(part.video_clip) };
|
|
318
|
-
if (part.speech != null) return { speech: omitKind(part.speech) };
|
|
319
|
-
if (part.caption != null) return { caption: omitKind(part.caption) };
|
|
320
|
-
return { bgm: omitKind(part.bgm) };
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* Rebuild a discriminated `PartUnion` from a draft part value: pick the one
|
|
324
|
-
* present sub-key, restore its `kind`, and drop the `$cid`. Returns `undefined`
|
|
325
|
-
* when no part sub-key is present (an empty/placeholder value).
|
|
326
|
-
*/
|
|
327
|
-
function draftToPartUnion(value) {
|
|
328
|
-
if (value == null) return void 0;
|
|
329
|
-
const v = value;
|
|
330
|
-
if (v.video_clip != null) return { video_clip: withKind(v.video_clip, "video_clip") };
|
|
331
|
-
if (v.speech != null) return { speech: withKind(v.speech, "speech") };
|
|
332
|
-
if (v.caption != null) return { caption: withKind(v.caption, "caption") };
|
|
333
|
-
if (v.bgm != null) return { bgm: withKind(v.bgm, "bgm") };
|
|
334
|
-
}
|
|
335
|
-
/** Drop `kind` (not stored) and `$cid` from a part payload for the draft. */
|
|
336
|
-
function omitKind(part) {
|
|
337
|
-
const { kind: _kind, $cid: _cid, ...rest } = part;
|
|
338
|
-
return rest;
|
|
339
|
-
}
|
|
340
|
-
/** Restore `kind` (and drop `$cid`) on a part sub-map read from the draft. */
|
|
341
|
-
function withKind(part, kind) {
|
|
342
|
-
const { $cid: _cid, ...rest } = part;
|
|
343
|
-
return {
|
|
344
|
-
...rest,
|
|
345
|
-
kind
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
/** Entries of a draft record (e.g. `tracks`) with `$cid` stripped. */
|
|
349
|
-
function recordEntries(record) {
|
|
350
|
-
if (record == null) return [];
|
|
351
|
-
const out = [];
|
|
352
|
-
for (const [key, value] of Object.entries(record)) {
|
|
353
|
-
if (key === "$cid") continue;
|
|
354
|
-
out.push([key, value]);
|
|
355
|
-
}
|
|
356
|
-
return out;
|
|
357
|
-
}
|
|
358
|
-
//#endregion
|
|
359
|
-
//#region src/document/types.ts
|
|
360
|
-
const VIDEO_DOCUMENT_SCHEMA_VERSION = "video-document/v0";
|
|
361
|
-
/** The linear speed multiplier of a `speed_shift`, defaulting to 1 (original). */
|
|
362
|
-
function speedOf(speedShift) {
|
|
363
|
-
const speed = speedShift?.config?.linear?.speed;
|
|
364
|
-
return typeof speed === "number" && Number.isFinite(speed) && speed > 0 ? speed : 1;
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* A video clip's effective timeline duration, derived from authoritative facts
|
|
368
|
-
* (RFC 02, `reference/16` §4, reference/17 §5): the trim window
|
|
369
|
-
* `play_out - play_in` divided by the speed multiplier, rounded to integer ms.
|
|
370
|
-
* `play_in` / `play_out` are optional in the IDL but every write path sets them
|
|
371
|
-
* (defaulting to the whole media), and the legacy-ingest projection backfills the
|
|
372
|
-
* window from a legacy `duration_ms` — so an authoritative clip always carries a
|
|
373
|
-
* trim window and there is no stored `duration_ms` to fall back to. A clip with
|
|
374
|
-
* neither bound yields 0. Speeds the clip up (>1× → shorter) or down (<1×).
|
|
375
|
-
*/
|
|
376
|
-
function effectiveVideoClipDurationMs(clip) {
|
|
377
|
-
const speed = speedOf(clip.speed_shift);
|
|
378
|
-
const sourceMs = (clip.play_out ?? 0) - (clip.play_in ?? 0);
|
|
379
|
-
const effective = Math.round(sourceMs / speed);
|
|
380
|
-
return Number.isFinite(effective) && effective > 0 ? effective : 0;
|
|
381
|
-
}
|
|
382
|
-
//#endregion
|
|
383
|
-
//#region src/timeline-core/types.ts
|
|
384
|
-
/** Total document duration when there is no real content (matches FE bgm fallback). */
|
|
385
|
-
const TIMELINE_SKELETON_DURATION_MS = 2e4;
|
|
386
|
-
/** Empty placeholder clip marker: a video_clip part with no backing media. */
|
|
387
|
-
function isEmptyVideoClip(part) {
|
|
388
|
-
const clip = part?.video_clip;
|
|
389
|
-
if (clip == null) return false;
|
|
390
|
-
return clip.origin_media_id == null || clip.origin_media_id === "";
|
|
391
|
-
}
|
|
392
|
-
/** Clamp a duration to a non-negative integer (NaN/Infinity/negative → 0). */
|
|
393
|
-
function safeDurationMs(value) {
|
|
394
|
-
const n = Number(value);
|
|
395
|
-
if (!Number.isFinite(n) || n <= 0) return 0;
|
|
396
|
-
return Math.round(n);
|
|
397
|
-
}
|
|
398
|
-
function partDurationMs(doc, partId) {
|
|
399
|
-
const part = doc.part_library[partId];
|
|
400
|
-
if (part?.video_clip != null) return safeDurationMs(effectiveVideoClipDurationMs(part.video_clip));
|
|
401
|
-
if (part?.bgm != null) return doc.timeline.duration_ms > 0 ? doc.timeline.duration_ms : TIMELINE_SKELETON_DURATION_MS;
|
|
402
|
-
if (part?.speech != null) return safeDurationMs(part.speech.media_duration_ms);
|
|
403
|
-
if (part?.caption != null) return safeDurationMs(part.caption.initial_duration_ms);
|
|
404
|
-
return 0;
|
|
405
|
-
}
|
|
406
|
-
//#endregion
|
|
407
|
-
//#region src/timeline-core/cascade.ts
|
|
408
|
-
/**
|
|
409
|
-
* Canonical timeline cascade primitives (ADR 0009).
|
|
410
|
-
*
|
|
411
|
-
* Single source of truth for "how an edit's connected regions move": main-track
|
|
412
|
-
* seamless layout, aggregation position sync + reassignment, total-duration
|
|
413
|
-
* recompute, speech-overlap resolution, gap filling. Reconciled per ADR 0009 §4:
|
|
414
|
-
*
|
|
415
|
-
* - product behavior follows the FE current implementation;
|
|
416
|
-
* - all times are integer ms — positions/durations are rounded, never floated;
|
|
417
|
-
* - function decomposition follows agent-harness (the Python-derived structure).
|
|
418
|
-
*
|
|
419
|
-
* Every function mutates the `TimelineDoc` in place (the editor runs them inside
|
|
420
|
-
* one immer `transact`, so in-place edits diff correctly).
|
|
421
|
-
*/
|
|
422
|
-
/**
|
|
423
|
-
* Lay the main track out head-to-tail from 0, rewriting each item's
|
|
424
|
-
* `abs_time_position`. Items whose part is missing from the library are dropped.
|
|
425
|
-
*/
|
|
426
|
-
function arrangeMainTrackSeamlessly(doc) {
|
|
427
|
-
const kept = [];
|
|
428
|
-
let cursor = 0;
|
|
429
|
-
for (const item of doc.main_track) {
|
|
430
|
-
if (doc.part_library[item.part_id] == null) continue;
|
|
431
|
-
item.abs_time_position = cursor;
|
|
432
|
-
cursor += partDurationMs(doc, item.part_id);
|
|
433
|
-
kept.push(item);
|
|
434
|
-
}
|
|
435
|
-
doc.main_track = kept;
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Sync attached parts' absolute positions from their host:
|
|
439
|
-
* speech.abs = host_video.abs + relative_time_position
|
|
440
|
-
* caption.abs = speech.abs + caption.start_ms
|
|
441
|
-
*/
|
|
442
|
-
function syncAggregatedClipsTimePosition(doc) {
|
|
443
|
-
const videoByPart = indexByPart(doc.main_track);
|
|
444
|
-
const speechByPart = indexByPart(doc.speech_track);
|
|
445
|
-
for (const aggregation of doc.aggregations) {
|
|
446
|
-
const videoItem = videoByPart.get(aggregation.body_part_id);
|
|
447
|
-
if (videoItem == null) continue;
|
|
448
|
-
for (const attachment of aggregation.attachments) {
|
|
449
|
-
const speechItem = speechByPart.get(attachment.part_id);
|
|
450
|
-
if (speechItem == null) continue;
|
|
451
|
-
speechItem.abs_time_position = videoItem.abs_time_position + attachment.relative_time_position;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
for (const captionItem of doc.caption_track) {
|
|
455
|
-
const captionPart = doc.part_library[captionItem.part_id]?.caption;
|
|
456
|
-
if (captionPart == null) continue;
|
|
457
|
-
const speechItem = captionPart.speech_part_id == null ? void 0 : speechByPart.get(captionPart.speech_part_id);
|
|
458
|
-
if (speechItem == null) continue;
|
|
459
|
-
captionItem.abs_time_position = speechItem.abs_time_position + safeDurationMs(captionPart.start_ms);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
/**
|
|
463
|
-
* Reassign each speech to the video clip whose time range contains its start,
|
|
464
|
-
* rebuilding `aggregations`. A speech before the first clip or after the last
|
|
465
|
-
* falls back to the first / last clip respectively (FE: see §4 note — FE falls
|
|
466
|
-
* back to last only; we keep the harness two-sided fallback because a speech
|
|
467
|
-
* dragged before clip 0 belonging to the last clip is clearly wrong, and the FE
|
|
468
|
-
* single-sided rule is an acknowledged rough edge). `relative_time_position` is
|
|
469
|
-
* clamped to a non-negative integer.
|
|
470
|
-
*/
|
|
471
|
-
function reassignSpeechesToVideoClipsByTime(doc) {
|
|
472
|
-
const ranges = doc.main_track.filter((item) => doc.part_library[item.part_id] != null).map((item) => ({
|
|
473
|
-
part_id: item.part_id,
|
|
474
|
-
start: item.abs_time_position,
|
|
475
|
-
end: item.abs_time_position + partDurationMs(doc, item.part_id)
|
|
476
|
-
}));
|
|
477
|
-
if (ranges.length === 0) {
|
|
478
|
-
doc.aggregations = [];
|
|
479
|
-
return;
|
|
480
|
-
}
|
|
481
|
-
const rebuilt = /* @__PURE__ */ new Map();
|
|
482
|
-
for (const speechItem of doc.speech_track) {
|
|
483
|
-
const start = speechItem.abs_time_position;
|
|
484
|
-
const targetRange = ranges.find((r) => r.start <= start && start < r.end) ?? (start < ranges[0].start ? ranges[0] : ranges[ranges.length - 1]);
|
|
485
|
-
const relative = Math.max(0, Math.round(start - targetRange.start));
|
|
486
|
-
const targetId = targetRange.part_id;
|
|
487
|
-
let aggregation = rebuilt.get(targetId);
|
|
488
|
-
if (aggregation == null) {
|
|
489
|
-
aggregation = {
|
|
490
|
-
body_part_id: targetId,
|
|
491
|
-
attachments: []
|
|
492
|
-
};
|
|
493
|
-
rebuilt.set(targetId, aggregation);
|
|
494
|
-
}
|
|
495
|
-
aggregation.attachments.push({
|
|
496
|
-
part_id: speechItem.part_id,
|
|
497
|
-
relative_time_position: relative
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
doc.aggregations = ranges.map((r) => rebuilt.get(r.part_id)).filter((a) => a != null);
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* Recompute `timeline.duration_ms` as the max end (abs + duration) across main,
|
|
504
|
-
* speech, and caption lanes (BGM does not extend the timeline).
|
|
505
|
-
*
|
|
506
|
-
* BGM has no authoritative duration (RFC 02 / `reference/16` §0b): its effective
|
|
507
|
-
* length is always the timeline total, so it is not written back here — the
|
|
508
|
-
* projection derives it from `timeline.duration_ms` on read (`partDurationMs`
|
|
509
|
-
* returns the timeline total for a bgm part). The empty-document 20s skeleton is
|
|
510
|
-
* applied at that read step, not stored.
|
|
511
|
-
*/
|
|
512
|
-
function recalculateTimelineDuration(doc) {
|
|
513
|
-
const max = Math.max(laneEndMs(doc, doc.main_track), laneEndMs(doc, doc.speech_track), laneEndMs(doc, doc.caption_track));
|
|
514
|
-
doc.timeline.duration_ms = max;
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
* Resolve one speech overlap by shifting the overlapping speech's host video
|
|
518
|
-
* (and every clip after it) right. Returns true when one overlap was resolved;
|
|
519
|
-
* callers loop until it returns false. The compared range is the speech merged
|
|
520
|
-
* with its captions: start = min(speech.start, captions.start) (FE behavior),
|
|
521
|
-
* end = max(speech.end, captions.end).
|
|
522
|
-
*/
|
|
523
|
-
function resolveSpeechOverlapByShiftingVideos(doc) {
|
|
524
|
-
const speeches = doc.speech_track;
|
|
525
|
-
for (let i = 1; i < speeches.length; i++) {
|
|
526
|
-
const prev = speechWithCaptionsRange(doc, speeches[i - 1]);
|
|
527
|
-
const curr = speechWithCaptionsRange(doc, speeches[i]);
|
|
528
|
-
if (prev == null || curr == null) continue;
|
|
529
|
-
if (curr.start >= prev.end) continue;
|
|
530
|
-
const overlapMs = Math.round(prev.end - curr.start);
|
|
531
|
-
const hostId = hostVideoOf(doc, speeches[i].part_id);
|
|
532
|
-
if (hostId == null) continue;
|
|
533
|
-
const fromIndex = doc.main_track.findIndex((item) => item.part_id === hostId);
|
|
534
|
-
if (fromIndex < 0) continue;
|
|
535
|
-
for (let idx = fromIndex; idx < doc.main_track.length; idx++) doc.main_track[idx].abs_time_position += overlapMs;
|
|
536
|
-
syncAggregatedClipsTimePosition(doc);
|
|
537
|
-
return true;
|
|
538
|
-
}
|
|
539
|
-
return false;
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* Resolve speech overlaps for one cascade pass. Mirrors the authoritative FE
|
|
543
|
-
* `ensureNoOverlappingClips`, which is documented to resolve AT MOST ONE overlap
|
|
544
|
-
* per cascade and is invoked exactly once at every FE call site — the supported
|
|
545
|
-
* ops each produce at most one new overlap. It is NOT a fixpoint loop: shifting a
|
|
546
|
-
* host right also moves every speech anchored to it, so two speeches sharing a
|
|
547
|
-
* host can never be separated by shifting. Looping to a "fixed point" there does
|
|
548
|
-
* not converge — it accumulates the same overlap every iteration and pushes the
|
|
549
|
-
* clip arbitrarily far right (e.g. a sped-up clip landing at ~287k ms instead of
|
|
550
|
-
* its seamless slot). A single pass matches FE product behavior and terminates.
|
|
551
|
-
*/
|
|
552
|
-
function resolveAllSpeechOverlaps(doc) {
|
|
553
|
-
resolveSpeechOverlapByShiftingVideos(doc);
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* Make the main track gapless by adjusting/merging empty placeholder clips or
|
|
557
|
-
* inserting new ones between real clips. Mirrors the harness four-case rule, but
|
|
558
|
-
* the merge of two adjacent empty clips keeps the earlier clip (harness Case 4).
|
|
559
|
-
* Requires `makeEmptyPart` to mint a placeholder part (the editor supplies an
|
|
560
|
-
* id generator).
|
|
561
|
-
*/
|
|
562
|
-
function fillMainTrackTimeGaps(doc, makeEmptyPart) {
|
|
563
|
-
const items = doc.main_track;
|
|
564
|
-
let i = 0;
|
|
565
|
-
while (i < items.length) {
|
|
566
|
-
const current = items[i];
|
|
567
|
-
const currentPart = doc.part_library[current.part_id];
|
|
568
|
-
if (currentPart == null) {
|
|
569
|
-
i += 1;
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
|
-
const currentEnd = current.abs_time_position + partDurationMs(doc, current.part_id);
|
|
573
|
-
if (i + 1 >= items.length) break;
|
|
574
|
-
const next = items[i + 1];
|
|
575
|
-
const nextPart = doc.part_library[next.part_id];
|
|
576
|
-
if (nextPart == null) {
|
|
577
|
-
i += 1;
|
|
578
|
-
continue;
|
|
579
|
-
}
|
|
580
|
-
const gap = next.abs_time_position - currentEnd;
|
|
581
|
-
if (gap > 0) {
|
|
582
|
-
if (isEmptyVideoClip(currentPart)) {
|
|
583
|
-
extendEmptyClip(currentPart.video_clip, gap);
|
|
584
|
-
continue;
|
|
585
|
-
}
|
|
586
|
-
if (isEmptyVideoClip(nextPart)) {
|
|
587
|
-
next.abs_time_position -= gap;
|
|
588
|
-
extendEmptyClip(nextPart.video_clip, gap);
|
|
589
|
-
i += 1;
|
|
590
|
-
continue;
|
|
591
|
-
}
|
|
592
|
-
const { partId } = makeEmptyPart(gap);
|
|
593
|
-
doc.part_library[partId] = { video_clip: {
|
|
594
|
-
id: partId,
|
|
595
|
-
kind: "video_clip",
|
|
596
|
-
play_in: 0,
|
|
597
|
-
play_out: gap,
|
|
598
|
-
volume: 0,
|
|
599
|
-
origin_media_id: ""
|
|
600
|
-
} };
|
|
601
|
-
items.splice(i + 1, 0, {
|
|
602
|
-
part_id: partId,
|
|
603
|
-
time_position: { mode: "sequential" },
|
|
604
|
-
abs_time_position: currentEnd
|
|
605
|
-
});
|
|
606
|
-
i += 1;
|
|
607
|
-
continue;
|
|
608
|
-
}
|
|
609
|
-
if (gap === 0 && isEmptyVideoClip(currentPart) && isEmptyVideoClip(nextPart)) {
|
|
610
|
-
extendEmptyClip(currentPart.video_clip, partDurationMs(doc, next.part_id));
|
|
611
|
-
items.splice(i + 1, 1);
|
|
612
|
-
delete doc.part_library[next.part_id];
|
|
613
|
-
continue;
|
|
614
|
-
}
|
|
615
|
-
i += 1;
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
function indexByPart(items) {
|
|
619
|
-
const map = /* @__PURE__ */ new Map();
|
|
620
|
-
for (const item of items) map.set(item.part_id, item);
|
|
621
|
-
return map;
|
|
622
|
-
}
|
|
623
|
-
function laneEndMs(doc, items) {
|
|
624
|
-
let max = 0;
|
|
625
|
-
for (const item of items) {
|
|
626
|
-
if (doc.part_library[item.part_id] == null) continue;
|
|
627
|
-
const end = item.abs_time_position + partDurationMs(doc, item.part_id);
|
|
628
|
-
if (end > max) max = end;
|
|
629
|
-
}
|
|
630
|
-
return max;
|
|
631
|
-
}
|
|
632
|
-
function extendEmptyClip(clip, byMs) {
|
|
633
|
-
clip.play_out = safeDurationMs(clip.play_out) + byMs;
|
|
634
|
-
}
|
|
635
|
-
/** speech merged with its captions: start = min, end = max. */
|
|
636
|
-
function speechWithCaptionsRange(doc, speechItem) {
|
|
637
|
-
const speech = doc.part_library[speechItem.part_id]?.speech;
|
|
638
|
-
if (speech == null) return null;
|
|
639
|
-
let start = speechItem.abs_time_position;
|
|
640
|
-
let end = speechItem.abs_time_position + safeDurationMs(speech.media_duration_ms);
|
|
641
|
-
for (const captionId of captionIdsOf(speech)) {
|
|
642
|
-
const captionItem = doc.caption_track.find((item) => item.part_id === captionId);
|
|
643
|
-
const captionPart = doc.part_library[captionId]?.caption;
|
|
644
|
-
if (captionItem == null || captionPart == null) continue;
|
|
645
|
-
const cStart = captionItem.abs_time_position;
|
|
646
|
-
const cEnd = captionItem.abs_time_position + safeDurationMs(captionPart.initial_duration_ms);
|
|
647
|
-
if (cStart < start) start = cStart;
|
|
648
|
-
if (cEnd > end) end = cEnd;
|
|
649
|
-
}
|
|
650
|
-
return {
|
|
651
|
-
start,
|
|
652
|
-
end
|
|
653
|
-
};
|
|
654
|
-
}
|
|
655
|
-
function captionIdsOf(speech) {
|
|
656
|
-
return (speech.caption_ids ?? []).filter((id) => id != null);
|
|
657
|
-
}
|
|
658
|
-
function hostVideoOf(doc, speechPartId) {
|
|
659
|
-
for (const aggregation of doc.aggregations) if (aggregation.attachments.some((att) => att.part_id === speechPartId)) return aggregation.body_part_id;
|
|
660
|
-
return null;
|
|
661
|
-
}
|
|
662
|
-
//#endregion
|
|
663
|
-
//#region src/timeline-core/entrypoints.ts
|
|
664
|
-
/**
|
|
665
|
-
* The full solve pipeline: arrange → sync → reassign → resolve-overlap →
|
|
666
|
-
* fill-gaps → recalc. The single cascade the read-side projection runs; ops
|
|
667
|
-
* never call it (they write only facts — RFC 02 §7/§10).
|
|
668
|
-
*/
|
|
669
|
-
function cascadeAfterVideoClipChanges(doc, makeEmptyPart) {
|
|
670
|
-
arrangeMainTrackSeamlessly(doc);
|
|
671
|
-
syncAggregatedClipsTimePosition(doc);
|
|
672
|
-
reassignSpeechesToVideoClipsByTime(doc);
|
|
673
|
-
resolveAllSpeechOverlaps(doc);
|
|
674
|
-
fillMainTrackTimeGaps(doc, makeEmptyPart);
|
|
675
|
-
recalculateTimelineDuration(doc);
|
|
676
|
-
}
|
|
677
|
-
//#endregion
|
|
678
|
-
//#region src/timeline-core/bridge.ts
|
|
679
|
-
/**
|
|
680
|
-
* Solve a `VideoDocument` (authoritative, position-only) into its derived
|
|
681
|
-
* read-view: absolute time per item, `part_aggregations`, and total duration.
|
|
682
|
-
* This is the read side of the single-directional flow — never written back.
|
|
683
|
-
*/
|
|
684
|
-
function solveVideoDocument(document) {
|
|
685
|
-
const doc = videoDocumentToTimelineDoc(document);
|
|
686
|
-
let counter = 0;
|
|
687
|
-
cascadeAfterVideoClipChanges(doc, () => ({ partId: `empty_${counter++}` }));
|
|
688
|
-
const absByPartId = /* @__PURE__ */ new Map();
|
|
689
|
-
for (const item of doc.main_track) absByPartId.set(item.part_id, item.abs_time_position);
|
|
690
|
-
for (const item of doc.speech_track) absByPartId.set(item.part_id, item.abs_time_position);
|
|
691
|
-
for (const item of doc.caption_track) absByPartId.set(item.part_id, item.abs_time_position);
|
|
692
|
-
for (const item of doc.bgm_track) absByPartId.set(item.part_id, item.abs_time_position);
|
|
693
|
-
return {
|
|
694
|
-
absByPartId,
|
|
695
|
-
aggregations: doc.aggregations.map((aggregation) => ({
|
|
696
|
-
body_part_id: aggregation.body_part_id,
|
|
697
|
-
attachments: aggregation.attachments.map((a) => ({
|
|
698
|
-
part_id: a.part_id,
|
|
699
|
-
relative_time_position: a.relative_time_position
|
|
700
|
-
}))
|
|
701
|
-
})),
|
|
702
|
-
durationMs: doc.timeline.duration_ms,
|
|
703
|
-
partLibrary: doc.part_library
|
|
704
|
-
};
|
|
705
|
-
}
|
|
706
|
-
function videoDocumentToTimelineDoc(document) {
|
|
707
|
-
const partLibrary = {};
|
|
708
|
-
for (const [partId, part] of Object.entries(document.part_library ?? {})) partLibrary[partId] = part;
|
|
709
|
-
const laneItems = (track) => (track?.items ?? []).map((item) => ({
|
|
710
|
-
part_id: item.part_id,
|
|
711
|
-
time_position: item.time_position,
|
|
712
|
-
abs_time_position: seedAbs(item.time_position, item.fallback_abs_ms),
|
|
713
|
-
fallback_abs_ms: item.fallback_abs_ms
|
|
714
|
-
}));
|
|
715
|
-
const tracks = document.tracks ?? [];
|
|
716
|
-
let mainItems = [];
|
|
717
|
-
let speechItems = [];
|
|
718
|
-
let bgmItems = [];
|
|
719
|
-
let captionItems = [];
|
|
720
|
-
for (const t of tracks) switch (t.parts_kind) {
|
|
721
|
-
case "video_clip":
|
|
722
|
-
mainItems = mainItems.concat(laneItems(t));
|
|
723
|
-
break;
|
|
724
|
-
case "speech":
|
|
725
|
-
speechItems = speechItems.concat(laneItems(t));
|
|
726
|
-
break;
|
|
727
|
-
case "bgm":
|
|
728
|
-
bgmItems = bgmItems.concat(laneItems(t));
|
|
729
|
-
break;
|
|
730
|
-
case "caption":
|
|
731
|
-
captionItems = captionItems.concat(laneItems(t));
|
|
732
|
-
break;
|
|
733
|
-
}
|
|
734
|
-
return {
|
|
735
|
-
main_track: mainItems,
|
|
736
|
-
speech_track: speechItems,
|
|
737
|
-
caption_track: captionItems,
|
|
738
|
-
bgm_track: bgmItems,
|
|
739
|
-
part_library: partLibrary,
|
|
740
|
-
aggregations: seedAggregations(mainItems, speechItems),
|
|
741
|
-
timeline: {
|
|
742
|
-
duration_ms: 0,
|
|
743
|
-
unit_time_ms: document.timeline?.unit_time_ms ?? 0
|
|
744
|
-
}
|
|
745
|
-
};
|
|
746
|
-
}
|
|
747
|
-
/**
|
|
748
|
-
* Seed an item's solve-variable `abs_time_position` from its `time_position` (and
|
|
749
|
-
* the `fallback_abs_ms` snapshot carried beside it): `absolute` → `offsetMs`;
|
|
750
|
-
* `anchored` → `fallbackAbsMs` (last solved position, also the orphan-recovery
|
|
751
|
-
* anchor when the host is gone); `sequential` → 0 (the cascade lays the main
|
|
752
|
-
* track out head-to-tail). The cascade then resolves anchored items against their
|
|
753
|
-
* host, so this seed only needs to put each item somewhere plausible for the
|
|
754
|
-
* first reassign/overlap pass.
|
|
755
|
-
*/
|
|
756
|
-
function seedAbs(position, fallbackAbsMs) {
|
|
757
|
-
switch (position.mode) {
|
|
758
|
-
case "absolute": return Math.round(position.offsetMs);
|
|
759
|
-
case "anchored": return Math.round(fallbackAbsMs ?? 0);
|
|
760
|
-
case "sequential": return 0;
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
/**
|
|
764
|
-
* Build the cascade `aggregations` parent-map from anchored items: an anchored
|
|
765
|
-
* item's `anchorPartId` is the host's `part_id` (= `body_part_id`), `offsetMs`
|
|
766
|
-
* is `relative_time_position`. Only main-track (video) hosts form aggregations;
|
|
767
|
-
* caption→speech relations are recomputed by the cascade from `caption.start_ms`.
|
|
768
|
-
*/
|
|
769
|
-
function seedAggregations(mainItems, speechItems) {
|
|
770
|
-
const mainPartIds = new Set(mainItems.map((item) => item.part_id));
|
|
771
|
-
const byHost = /* @__PURE__ */ new Map();
|
|
772
|
-
const order = [];
|
|
773
|
-
for (const speech of speechItems) {
|
|
774
|
-
if (speech.time_position.mode !== "anchored") continue;
|
|
775
|
-
const host = speech.time_position.anchorPartId;
|
|
776
|
-
if (!mainPartIds.has(host)) continue;
|
|
777
|
-
let aggregation = byHost.get(host);
|
|
778
|
-
if (aggregation == null) {
|
|
779
|
-
aggregation = {
|
|
780
|
-
body_part_id: host,
|
|
781
|
-
attachments: []
|
|
782
|
-
};
|
|
783
|
-
byHost.set(host, aggregation);
|
|
784
|
-
order.push(host);
|
|
785
|
-
}
|
|
786
|
-
aggregation.attachments.push({
|
|
787
|
-
part_id: speech.part_id,
|
|
788
|
-
relative_time_position: speech.time_position.offsetMs
|
|
789
|
-
});
|
|
790
|
-
}
|
|
791
|
-
return order.map((host) => byHost.get(host));
|
|
792
|
-
}
|
|
793
|
-
/**
|
|
794
|
-
* Lane-stacking rank for the single `tracks` list (reference/17 §4): caption
|
|
795
|
-
* (above) sits before the video_clip main track, which sits before speech / bgm
|
|
796
|
-
* (below). The `tracks` array is kept in this top-to-bottom order so a freshly
|
|
797
|
-
* minted track lands in the right place and the projection's three-pane rebuild
|
|
798
|
-
* stays deterministic.
|
|
799
|
-
*/
|
|
800
|
-
function laneRank(kind) {
|
|
801
|
-
switch (kind) {
|
|
802
|
-
case "caption": return 0;
|
|
803
|
-
case "video_clip": return 1;
|
|
804
|
-
case "speech":
|
|
805
|
-
case "bgm": return 2;
|
|
806
|
-
default: return 3;
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
/**
|
|
810
|
-
* Insert a freshly-minted track into `tracks` at the position that keeps the
|
|
811
|
-
* lane-stacking order (caption → main → speech/bgm). Inserts before the first
|
|
812
|
-
* track whose rank is strictly greater, so same-rank tracks keep insertion order.
|
|
813
|
-
*/
|
|
814
|
-
function insertTrackByLaneOrder(tracks, track) {
|
|
815
|
-
const rank = laneRank(track.parts_kind);
|
|
816
|
-
const at = tracks.findIndex((t) => laneRank(t?.parts_kind) > rank);
|
|
817
|
-
if (at < 0) tracks.push(track);
|
|
818
|
-
else tracks.splice(at, 0, track);
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* Locate a lane's track row in the single `tracks` list by kind, minting an empty
|
|
822
|
-
* row in lane-stacking order if absent (reference/17 §4: lane = `parts_kind`).
|
|
823
|
-
* Ops use this to write authoritative items onto the right lane. The track id
|
|
824
|
-
* mirrors the seed convention (`<kind>_track`).
|
|
825
|
-
*/
|
|
826
|
-
function ensureLaneTrack(draft, kind) {
|
|
827
|
-
draft.tracks ??= [];
|
|
828
|
-
let track = draft.tracks.find((t) => t?.parts_kind === kind);
|
|
829
|
-
if (track == null) {
|
|
830
|
-
track = {
|
|
831
|
-
id: kind === "video_clip" ? "main_track" : `${kind}_track`,
|
|
832
|
-
parts_kind: kind,
|
|
833
|
-
is_hidden: void 0,
|
|
834
|
-
items: []
|
|
835
|
-
};
|
|
836
|
-
insertTrackByLaneOrder(draft.tracks, track);
|
|
837
|
-
}
|
|
838
|
-
track.items ??= [];
|
|
839
|
-
return track;
|
|
840
|
-
}
|
|
841
|
-
/** Find a secondary lane's track row without minting it. */
|
|
842
|
-
function findLaneTrack(draft, kind) {
|
|
843
|
-
return (draft.tracks ?? []).find((t) => t?.parts_kind === kind);
|
|
844
|
-
}
|
|
845
|
-
//#endregion
|
|
846
|
-
//#region src/document/zod-schema.ts
|
|
847
|
-
const partKindSchema = z.enum([
|
|
848
|
-
"video_clip",
|
|
849
|
-
"speech",
|
|
850
|
-
"caption",
|
|
851
|
-
"bgm"
|
|
852
|
-
]);
|
|
853
|
-
const finiteNumber = z.number().finite();
|
|
854
|
-
const trackItemTimePositionSchema = z.discriminatedUnion("mode", [
|
|
855
|
-
z.object({ mode: z.literal("sequential") }).passthrough(),
|
|
856
|
-
z.object({
|
|
857
|
-
mode: z.literal("anchored"),
|
|
858
|
-
anchorPartId: z.string(),
|
|
859
|
-
offsetMs: finiteNumber
|
|
860
|
-
}).passthrough(),
|
|
861
|
-
z.object({
|
|
862
|
-
mode: z.literal("absolute"),
|
|
863
|
-
offsetMs: finiteNumber
|
|
864
|
-
}).passthrough()
|
|
865
|
-
]);
|
|
866
|
-
const trackItemSchema = z.object({
|
|
867
|
-
part_id: z.string(),
|
|
868
|
-
time_position: trackItemTimePositionSchema,
|
|
869
|
-
fallback_abs_ms: finiteNumber.optional()
|
|
870
|
-
}).passthrough();
|
|
871
|
-
const trackSchema = z.object({
|
|
872
|
-
id: z.string().optional(),
|
|
873
|
-
parts_kind: partKindSchema.optional(),
|
|
874
|
-
is_hidden: z.boolean().optional(),
|
|
875
|
-
items: z.array(trackItemSchema).optional()
|
|
876
|
-
}).passthrough();
|
|
877
|
-
const speedShiftSchema$1 = z.object({
|
|
878
|
-
category: z.string().optional(),
|
|
879
|
-
mode: z.string().optional(),
|
|
880
|
-
config: z.object({ linear: z.object({ speed: finiteNumber.optional() }).passthrough().optional() }).passthrough().optional()
|
|
881
|
-
}).passthrough();
|
|
882
|
-
const videoClipPartSchema = z.object({
|
|
883
|
-
id: z.string().optional(),
|
|
884
|
-
kind: z.literal("video_clip"),
|
|
885
|
-
duration_ms: finiteNumber.optional(),
|
|
886
|
-
play_in: finiteNumber,
|
|
887
|
-
play_out: finiteNumber,
|
|
888
|
-
volume: finiteNumber,
|
|
889
|
-
origin_media_id: z.string(),
|
|
890
|
-
speed_shift: speedShiftSchema$1.optional()
|
|
891
|
-
}).passthrough();
|
|
892
|
-
const speechPartSchema = z.object({
|
|
893
|
-
id: z.string().optional(),
|
|
894
|
-
kind: z.literal("speech"),
|
|
895
|
-
media_duration_ms: finiteNumber,
|
|
896
|
-
duration_ms: finiteNumber.optional(),
|
|
897
|
-
audio_script: z.string(),
|
|
898
|
-
volume: finiteNumber,
|
|
899
|
-
audio_storage_key: z.string(),
|
|
900
|
-
origin_speech_id: z.string(),
|
|
901
|
-
voice: z.unknown(),
|
|
902
|
-
caption_ids: z.array(z.string())
|
|
903
|
-
}).passthrough();
|
|
904
|
-
const captionPartSchema = z.object({
|
|
905
|
-
id: z.string().optional(),
|
|
906
|
-
kind: z.literal("caption"),
|
|
907
|
-
initial_duration_ms: finiteNumber,
|
|
908
|
-
speech_part_id: z.string(),
|
|
909
|
-
text: z.string(),
|
|
910
|
-
start_ms: finiteNumber,
|
|
911
|
-
style: z.object({
|
|
912
|
-
font_id: z.string().optional(),
|
|
913
|
-
font_size: finiteNumber.optional(),
|
|
914
|
-
font_color: z.string().optional(),
|
|
915
|
-
font_weight: finiteNumber.optional(),
|
|
916
|
-
entrance_animation: z.string().optional(),
|
|
917
|
-
entrance_animation_duration_ms: finiteNumber.optional(),
|
|
918
|
-
stroke_color: z.string().optional(),
|
|
919
|
-
stroke_width: finiteNumber.optional(),
|
|
920
|
-
position_x: finiteNumber.optional(),
|
|
921
|
-
position_y: finiteNumber.optional()
|
|
922
|
-
}).passthrough().optional()
|
|
923
|
-
}).passthrough();
|
|
924
|
-
const bgmPartSchema = z.object({
|
|
925
|
-
id: z.string().optional(),
|
|
926
|
-
kind: z.literal("bgm"),
|
|
927
|
-
audio_storage_key: z.string(),
|
|
928
|
-
volume: finiteNumber,
|
|
929
|
-
origin_media_id: z.string()
|
|
930
|
-
}).passthrough();
|
|
931
|
-
const partUnionSchema = z.union([
|
|
932
|
-
z.object({ video_clip: videoClipPartSchema }).passthrough(),
|
|
933
|
-
z.object({ speech: speechPartSchema }).passthrough(),
|
|
934
|
-
z.object({ caption: captionPartSchema }).passthrough(),
|
|
935
|
-
z.object({ bgm: bgmPartSchema }).passthrough()
|
|
936
|
-
]).refine((part) => {
|
|
937
|
-
const p = part;
|
|
938
|
-
return [
|
|
939
|
-
"video_clip",
|
|
940
|
-
"speech",
|
|
941
|
-
"caption",
|
|
942
|
-
"bgm"
|
|
943
|
-
].filter((k) => p[k] != null).length === 1;
|
|
944
|
-
}, { message: "A part_library value must have exactly one of video_clip / speech / caption / bgm" });
|
|
945
|
-
const videoDocumentMetaSchema = z.object({
|
|
946
|
-
schema_version: z.literal(VIDEO_DOCUMENT_SCHEMA_VERSION),
|
|
947
|
-
draft_id: z.string().optional(),
|
|
948
|
-
project_id: z.string().optional(),
|
|
949
|
-
owner_id: z.string().optional(),
|
|
950
|
-
thumbnail_storage_key: z.string().optional(),
|
|
951
|
-
chat_session_id: z.string().optional(),
|
|
952
|
-
video_creation_settings: z.unknown().optional(),
|
|
953
|
-
version: finiteNumber.optional()
|
|
954
|
-
}).passthrough();
|
|
955
|
-
const videoDocumentSchema = z.object({
|
|
956
|
-
meta: videoDocumentMetaSchema,
|
|
957
|
-
timeline: z.object({ unit_time_ms: finiteNumber.optional() }).passthrough().optional(),
|
|
958
|
-
tracks: z.array(trackSchema).optional(),
|
|
959
|
-
part_library: z.record(z.string(), partUnionSchema).optional()
|
|
960
|
-
}).passthrough().refine((doc) => (doc.tracks ?? []).filter((t) => t.parts_kind === "video_clip").length <= 1, {
|
|
961
|
-
message: "A VideoDocument may have at most one video_clip (main) track",
|
|
962
|
-
path: ["tracks"]
|
|
963
|
-
});
|
|
964
|
-
//#endregion
|
|
965
|
-
//#region src/document/validation.ts
|
|
966
|
-
/**
|
|
967
|
-
* Business-level schema guard for `VideoDocument` (RFC 03 §9). It is the gate
|
|
968
|
-
* that decides whether an arbitrary value is a *legal* `VideoDocument` before it
|
|
969
|
-
* is written into Loro — distinct from two neighbours:
|
|
970
|
-
*
|
|
971
|
-
* - `zod-schema.ts` (`videoDocumentSchema`) checks structure/shape only; this
|
|
972
|
-
* file layers the business rules on top (part-kind match, reference integrity,
|
|
973
|
-
* `position.anchorPartId` targets, value ranges, identity uniqueness).
|
|
974
|
-
* - loro-mirror's own `validateSchema` (run on every `setState`) only checks the
|
|
975
|
-
* storage structure, never these business invariants.
|
|
976
|
-
*
|
|
977
|
-
* Projection (`projection.ts`) calls `assertValidVideoDocument` before solving a
|
|
978
|
-
* document into the legacy `VideoDraft`.
|
|
979
|
-
*/
|
|
980
|
-
var VideoDocumentValidationError = class extends Error {
|
|
981
|
-
issues;
|
|
982
|
-
constructor(issues) {
|
|
983
|
-
super(`Invalid VideoDocument: ${issues.map((issue) => issue.message).join("; ")}`);
|
|
984
|
-
this.issues = issues;
|
|
985
|
-
this.name = "VideoDocumentValidationError";
|
|
986
|
-
}
|
|
987
|
-
};
|
|
988
|
-
/**
|
|
989
|
-
* Assert the document is legal enough to project. Only `error`-severity issues
|
|
990
|
-
* hard-reject; `recoverable` ones (dangling anchor / orphan, RFC 02 §11.1) are
|
|
991
|
-
* left for the projection to heal on read and do NOT throw. Use
|
|
992
|
-
* `validateVideoDocument` directly to inspect recoverable issues too.
|
|
993
|
-
*/
|
|
994
|
-
function assertValidVideoDocument(document) {
|
|
995
|
-
const blocking = validateVideoDocument(document).filter((issue) => (issue.severity ?? "error") === "error");
|
|
996
|
-
if (blocking.length > 0) throw new VideoDocumentValidationError(blocking);
|
|
997
|
-
}
|
|
998
|
-
function validateVideoDocument(document) {
|
|
999
|
-
const parsed = videoDocumentSchema.safeParse(document);
|
|
1000
|
-
if (!parsed.success) return parsed.error.issues.map((issue) => ({
|
|
1001
|
-
code: "invalid_schema",
|
|
1002
|
-
path: zodPath(issue.path),
|
|
1003
|
-
message: issue.message
|
|
1004
|
-
}));
|
|
1005
|
-
const doc = parsed.data;
|
|
1006
|
-
const issues = [];
|
|
1007
|
-
const partLibrary = doc.part_library ?? {};
|
|
1008
|
-
for (const [partId, part] of Object.entries(partLibrary)) {
|
|
1009
|
-
if (!partUnionSchema.safeParse(part).success) {
|
|
1010
|
-
issues.push({
|
|
1011
|
-
code: "unknown_part_kind",
|
|
1012
|
-
path: `/part_library/${partId}`,
|
|
1013
|
-
message: `Part "${partId}" is not a supported part union`
|
|
1014
|
-
});
|
|
1015
|
-
continue;
|
|
1016
|
-
}
|
|
1017
|
-
const wrapperKind = getPartUnionKind(part);
|
|
1018
|
-
const innerKind = getPartKind(part);
|
|
1019
|
-
if (wrapperKind != null && innerKind != null && wrapperKind !== innerKind) issues.push({
|
|
1020
|
-
code: "part_kind_mismatch",
|
|
1021
|
-
path: `/part_library/${partId}`,
|
|
1022
|
-
message: `Part "${partId}" wrapper kind "${wrapperKind}" does not match inner kind "${innerKind}"`
|
|
1023
|
-
});
|
|
1024
|
-
validatePartValues(partId, part, issues);
|
|
1025
|
-
}
|
|
1026
|
-
for (const [idx, track] of (doc.tracks ?? []).entries()) validateTrack(track, `tracks/${idx}`, partLibrary, issues, track.parts_kind === "video_clip");
|
|
1027
|
-
validateSpeechCaptionReferences(partLibrary, issues);
|
|
1028
|
-
validatePositionReferences(doc, partLibrary, issues);
|
|
1029
|
-
return issues;
|
|
1030
|
-
}
|
|
1031
|
-
/**
|
|
1032
|
-
* An `anchored` item whose `anchorPartId` no longer exists in the library is the
|
|
1033
|
-
* orphan condition (RFC 02 §4/§11.1) — e.g. a speech whose host video, or a
|
|
1034
|
-
* caption whose host speech, was concurrently deleted while this item was being
|
|
1035
|
-
* reparented in. This is flagged as a **recoverable** issue, NOT a hard error:
|
|
1036
|
-
* the projection heals it on read (the item's `fallback_abs_ms` snapshot seeds
|
|
1037
|
-
* its absolute position, then the cascade reassigns it to the nearest available
|
|
1038
|
-
* host, or it stays put as an absolute item). Reporting it here keeps the orphan
|
|
1039
|
-
* observable without blocking projection — the opposite of a hard reject, which
|
|
1040
|
-
* would make an inevitable concurrent-delete outcome un-projectable (§11.1).
|
|
1041
|
-
*/
|
|
1042
|
-
function validatePositionReferences(doc, partLibrary, issues) {
|
|
1043
|
-
const tracks = (doc.tracks ?? []).map((track, idx) => ({
|
|
1044
|
-
track,
|
|
1045
|
-
path: `tracks/${idx}`
|
|
1046
|
-
}));
|
|
1047
|
-
for (const { track, path } of tracks) for (const [idx, item] of (track?.items ?? []).entries()) {
|
|
1048
|
-
if (item.time_position.mode !== "anchored") continue;
|
|
1049
|
-
if (partLibrary[item.time_position.anchorPartId] == null) issues.push({
|
|
1050
|
-
code: "invalid_position_anchor",
|
|
1051
|
-
path: `/${path}/items/${idx}/time_position/anchorPartId`,
|
|
1052
|
-
message: `Track item "${path}/${idx}" anchors to missing part "${item.time_position.anchorPartId}"`,
|
|
1053
|
-
severity: "recoverable"
|
|
1054
|
-
});
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
function validatePartValues(partId, part, issues) {
|
|
1058
|
-
const payload = part.video_clip ?? part.speech ?? part.caption ?? part.bgm ?? void 0;
|
|
1059
|
-
if (payload == null) return;
|
|
1060
|
-
if ("duration_ms" in payload && payload.duration_ms != null && payload.duration_ms < 0) issues.push({
|
|
1061
|
-
code: "invalid_part_value",
|
|
1062
|
-
path: `/part_library/${partId}/duration_ms`,
|
|
1063
|
-
message: `Part "${partId}" has negative duration_ms`
|
|
1064
|
-
});
|
|
1065
|
-
if ("volume" in payload && payload.volume != null && !Number.isFinite(payload.volume)) issues.push({
|
|
1066
|
-
code: "invalid_part_value",
|
|
1067
|
-
path: `/part_library/${partId}/volume`,
|
|
1068
|
-
message: `Part "${partId}" has non-finite volume`
|
|
1069
|
-
});
|
|
1070
|
-
if (part.video_clip != null) {
|
|
1071
|
-
const { play_in, play_out } = part.video_clip;
|
|
1072
|
-
if (play_in != null && play_in < 0) issues.push({
|
|
1073
|
-
code: "invalid_part_value",
|
|
1074
|
-
path: `/part_library/${partId}/play_in`,
|
|
1075
|
-
message: `Video clip "${partId}" has negative play_in`
|
|
1076
|
-
});
|
|
1077
|
-
if (play_out != null && play_in != null && play_out < play_in) issues.push({
|
|
1078
|
-
code: "invalid_part_value",
|
|
1079
|
-
path: `/part_library/${partId}/play_out`,
|
|
1080
|
-
message: `Video clip "${partId}" has play_out before play_in`
|
|
1081
|
-
});
|
|
1082
|
-
}
|
|
1083
|
-
}
|
|
1084
|
-
function validateTrack(track, path, partLibrary, issues, isMainTrack) {
|
|
1085
|
-
if (track == null) return;
|
|
1086
|
-
const seenPartIds = /* @__PURE__ */ new Set();
|
|
1087
|
-
for (const [idx, item] of (track.items ?? []).entries()) {
|
|
1088
|
-
const partId = item.part_id;
|
|
1089
|
-
if (partId != null && partId !== "") if (seenPartIds.has(partId)) issues.push({
|
|
1090
|
-
code: "duplicate_track_item_identity",
|
|
1091
|
-
path: `/${path}/items/${idx}/part_id`,
|
|
1092
|
-
message: `Track item "${path}/${idx}" reuses part_id "${partId}"`
|
|
1093
|
-
});
|
|
1094
|
-
else seenPartIds.add(partId);
|
|
1095
|
-
if (partId == null || partId === "") {
|
|
1096
|
-
issues.push({
|
|
1097
|
-
code: "missing_part_reference",
|
|
1098
|
-
path: `/${path}/items/${idx}/part_id`,
|
|
1099
|
-
message: `Track item "${path}/${idx}" has no part_id`
|
|
1100
|
-
});
|
|
1101
|
-
continue;
|
|
1102
|
-
}
|
|
1103
|
-
const part = partLibrary[partId];
|
|
1104
|
-
if (part == null) {
|
|
1105
|
-
issues.push({
|
|
1106
|
-
code: "missing_part_reference",
|
|
1107
|
-
path: `/${path}/items/${idx}/part_id`,
|
|
1108
|
-
message: `Track item "${path}/${idx}" references missing part "${partId}"`
|
|
1109
|
-
});
|
|
1110
|
-
continue;
|
|
1111
|
-
}
|
|
1112
|
-
const partKind = getPartKind(part);
|
|
1113
|
-
if (isMainTrack && partKind !== "video_clip") issues.push({
|
|
1114
|
-
code: "main_track_non_video_clip",
|
|
1115
|
-
path: `/${path}/items/${idx}/part_id`,
|
|
1116
|
-
message: `Main track item "${path}/${idx}" references non-video part "${partId}"`
|
|
1117
|
-
});
|
|
1118
|
-
if (track.parts_kind != null && partKind != null && track.parts_kind !== partKind) issues.push({
|
|
1119
|
-
code: "track_kind_mismatch",
|
|
1120
|
-
path: `/${path}/items/${idx}/part_id`,
|
|
1121
|
-
message: `Track "${path}" expects "${track.parts_kind}" but part "${partId}" is "${partKind}"`
|
|
1122
|
-
});
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
/**
|
|
1126
|
-
* Check speech↔caption references. A reference to a *missing* part (speech's
|
|
1127
|
-
* caption_ids → deleted caption, or caption's speech_part_id → deleted speech)
|
|
1128
|
-
* is the orphan condition (RFC 02 §11.1): reported as **recoverable** so the
|
|
1129
|
-
* projection can heal it on read, not block. A *back-pointer mismatch* (caption
|
|
1130
|
-
* exists but does not point back) is data corruption, kept as a hard error.
|
|
1131
|
-
*/
|
|
1132
|
-
function validateSpeechCaptionReferences(partLibrary, issues) {
|
|
1133
|
-
for (const [partId, part] of Object.entries(partLibrary)) {
|
|
1134
|
-
if (part.speech != null) for (const captionId of part.speech.caption_ids ?? []) {
|
|
1135
|
-
const caption = partLibrary[captionId]?.caption;
|
|
1136
|
-
if (caption == null) {
|
|
1137
|
-
issues.push({
|
|
1138
|
-
code: "invalid_speech_caption_reference",
|
|
1139
|
-
path: `/part_library/${partId}/speech/caption_ids`,
|
|
1140
|
-
message: `Speech "${partId}" references missing caption "${captionId}"`,
|
|
1141
|
-
severity: "recoverable"
|
|
1142
|
-
});
|
|
1143
|
-
continue;
|
|
1144
|
-
}
|
|
1145
|
-
if (caption.speech_part_id !== partId) issues.push({
|
|
1146
|
-
code: "invalid_speech_caption_reference",
|
|
1147
|
-
path: `/part_library/${captionId}/caption/speech_part_id`,
|
|
1148
|
-
message: `Caption "${captionId}" does not point back to speech "${partId}"`
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
if (part.caption != null) {
|
|
1152
|
-
const speechId = part.caption.speech_part_id;
|
|
1153
|
-
if (speechId != null && partLibrary[speechId]?.speech == null) issues.push({
|
|
1154
|
-
code: "invalid_speech_caption_reference",
|
|
1155
|
-
path: `/part_library/${partId}/caption/speech_part_id`,
|
|
1156
|
-
message: `Caption "${partId}" references missing speech "${speechId}"`,
|
|
1157
|
-
severity: "recoverable"
|
|
1158
|
-
});
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1162
|
-
function getPartUnionKind(part) {
|
|
1163
|
-
if (part.video_clip != null) return "video_clip";
|
|
1164
|
-
if (part.speech != null) return "speech";
|
|
1165
|
-
if (part.caption != null) return "caption";
|
|
1166
|
-
if (part.bgm != null) return "bgm";
|
|
1167
|
-
}
|
|
1168
|
-
function getPartKind(part) {
|
|
1169
|
-
return part.video_clip?.kind ?? part.speech?.kind ?? part.caption?.kind ?? part.bgm?.kind;
|
|
1170
|
-
}
|
|
1171
|
-
function zodPath(path) {
|
|
1172
|
-
if (path.length === 0) return "/";
|
|
1173
|
-
return `/${path.map(String).join("/")}`;
|
|
1174
|
-
}
|
|
1175
|
-
//#endregion
|
|
1176
|
-
//#region src/document/projection.ts
|
|
1177
|
-
/**
|
|
1178
|
-
* Projection between the authoritative `VideoDocument` and the legacy
|
|
1179
|
-
* `VideoDraft` read-view (RFC 02 §5/§7). Both directions live here:
|
|
1180
|
-
*
|
|
1181
|
-
* - `toVideoDocument` ingests a `VideoDraft`, deriving each item's `position`
|
|
1182
|
-
* from the legacy absolute layout + aggregations; derived values (abs time,
|
|
1183
|
-
* `part_aggregations`, total duration) are dropped.
|
|
1184
|
-
* - `fromVideoDocument` solves a `VideoDocument` back into a `VideoDraft` via the
|
|
1185
|
-
* timeline-core cascade, re-deriving exactly those values.
|
|
1186
|
-
*
|
|
1187
|
-
* Business validation lives in `validation.ts`; `fromVideoDocument` asserts a
|
|
1188
|
-
* valid document before solving.
|
|
1189
|
-
*/
|
|
1190
|
-
/**
|
|
1191
|
-
* Ingest the legacy `VideoDraft` into the authoritative `VideoDocument`,
|
|
1192
|
-
* deriving each item's `time_position` from the legacy absolute layout +
|
|
1193
|
-
* aggregations (RFC 02 §4/§5). Absolute time, `part_aggregations`, and total
|
|
1194
|
-
* duration are dropped — they are re-derived by the projection.
|
|
1195
|
-
*/
|
|
1196
|
-
function toVideoDocument(draft) {
|
|
1197
|
-
const speechHost = buildSpeechHostMap(draft.part_aggregations);
|
|
1198
|
-
const partLibrary = draft.part_library ?? {};
|
|
1199
|
-
const toTrack = (track, isMain) => {
|
|
1200
|
-
if (track == null) return void 0;
|
|
1201
|
-
return {
|
|
1202
|
-
id: track.id,
|
|
1203
|
-
parts_kind: track.parts_kind,
|
|
1204
|
-
is_hidden: track.is_hidden,
|
|
1205
|
-
items: (track.items ?? []).map((item) => deriveItem(item, isMain, speechHost, partLibrary))
|
|
1206
|
-
};
|
|
1207
|
-
};
|
|
1208
|
-
const mainTrack = toTrack(draft.main_track, true);
|
|
1209
|
-
const tracks = [
|
|
1210
|
-
...(draft.above_main_tracks ?? []).map((t) => toTrack(t, false)),
|
|
1211
|
-
...mainTrack == null ? [] : [mainTrack],
|
|
1212
|
-
...(draft.below_main_tracks ?? []).map((t) => toTrack(t, false))
|
|
1213
|
-
];
|
|
1214
|
-
return {
|
|
1215
|
-
meta: {
|
|
1216
|
-
schema_version: VIDEO_DOCUMENT_SCHEMA_VERSION,
|
|
1217
|
-
draft_id: draft.id,
|
|
1218
|
-
project_id: draft.project_id,
|
|
1219
|
-
owner_id: draft.owner_id,
|
|
1220
|
-
thumbnail_storage_key: draft.thumbnail_storage_key,
|
|
1221
|
-
chat_session_id: draft.chat_session_id,
|
|
1222
|
-
video_creation_settings: clone(draft.video_creation_settings),
|
|
1223
|
-
version: draft.version
|
|
1224
|
-
},
|
|
1225
|
-
timeline: draft.timeline == null ? void 0 : { unit_time_ms: draft.timeline.unit_time_ms },
|
|
1226
|
-
tracks,
|
|
1227
|
-
part_library: toAuthoritativePartLibrary(draft.part_library)
|
|
1228
|
-
};
|
|
1229
|
-
}
|
|
1230
|
-
/**
|
|
1231
|
-
* Map the legacy `VideoDraft` part library into the authoritative shape
|
|
1232
|
-
* (reference/17 §5): the authoritative parts store no derived part-level
|
|
1233
|
-
* duration, so the read-view `duration_ms` is dropped from video / speech / bgm.
|
|
1234
|
-
* For a caption the legacy `duration_ms` is the generation-time length, stored
|
|
1235
|
-
* authoritatively as `initial_duration_ms`.
|
|
1236
|
-
*/
|
|
1237
|
-
function toAuthoritativePartLibrary(partLibrary) {
|
|
1238
|
-
if (partLibrary == null) return void 0;
|
|
1239
|
-
const out = {};
|
|
1240
|
-
for (const [partId, part] of Object.entries(partLibrary)) {
|
|
1241
|
-
if (typeof part !== "object" || part == null) continue;
|
|
1242
|
-
if (part.video_clip != null) out[partId] = { video_clip: withTrimWindowFromDuration(part.video_clip) };
|
|
1243
|
-
else if (part.speech != null) {
|
|
1244
|
-
const { duration_ms, rest } = splitDurationMs(part.speech);
|
|
1245
|
-
out[partId] = { speech: {
|
|
1246
|
-
...rest,
|
|
1247
|
-
media_duration_ms: rest.media_duration_ms ?? duration_ms
|
|
1248
|
-
} };
|
|
1249
|
-
} else if (part.caption != null) {
|
|
1250
|
-
const { duration_ms, rest } = splitDurationMs(part.caption);
|
|
1251
|
-
out[partId] = { caption: {
|
|
1252
|
-
...rest,
|
|
1253
|
-
initial_duration_ms: duration_ms
|
|
1254
|
-
} };
|
|
1255
|
-
} else if (part.bgm != null) out[partId] = { bgm: omitDurationMs(part.bgm) };
|
|
1256
|
-
}
|
|
1257
|
-
return out;
|
|
1258
|
-
}
|
|
1259
|
-
/**
|
|
1260
|
-
* Build a speech-host map from a part_aggregations list. Used by
|
|
1261
|
-
* `toVideoDocument` (legacy VideoDraft → VideoDocument) to recover each speech's
|
|
1262
|
-
* host video clip and relative offset. Aggregation items with null ids are
|
|
1263
|
-
* skipped (malformed input tolerance).
|
|
1264
|
-
*/
|
|
1265
|
-
function buildSpeechHostMap(aggregations) {
|
|
1266
|
-
const map = /* @__PURE__ */ new Map();
|
|
1267
|
-
for (const aggregation of aggregations ?? []) {
|
|
1268
|
-
const host = aggregation.body_part_id;
|
|
1269
|
-
if (host == null) continue;
|
|
1270
|
-
for (const attachment of aggregation.attachments ?? []) {
|
|
1271
|
-
if (attachment.part_id == null) continue;
|
|
1272
|
-
map.set(attachment.part_id, {
|
|
1273
|
-
hostPartId: host,
|
|
1274
|
-
offsetMs: Math.round(attachment.relative_time_position ?? 0)
|
|
1275
|
-
});
|
|
1276
|
-
}
|
|
1277
|
-
}
|
|
1278
|
-
return map;
|
|
1279
|
-
}
|
|
1280
|
-
/**
|
|
1281
|
-
* Derive `time_position` (and `fallbackAbsMs` for anchored items) from a legacy
|
|
1282
|
-
* absolute time + pre-built speech-host map + part library. The three-branch
|
|
1283
|
-
* rule (RFC 02 §4/§5, reference/17 §3):
|
|
1284
|
-
*
|
|
1285
|
-
* 1. main-track → `sequential`
|
|
1286
|
-
* 2. speech/attachment (part_id in speechHost) → `anchored(host, offsetMs)`
|
|
1287
|
-
* 3. caption (part has `speech_part_id`) → `anchored(speech, start_ms)`
|
|
1288
|
-
* 4. everything else → `absolute(abs)`
|
|
1289
|
-
*
|
|
1290
|
-
* `partLibrary` values may be `PartUnion | string | undefined` (draft raw
|
|
1291
|
-
* form); only object-typed entries are inspected for `caption`.
|
|
1292
|
-
*/
|
|
1293
|
-
function derivePositionFromAbs(partId, abs, isMain, speechHost, partLibrary) {
|
|
1294
|
-
if (isMain) return { timePosition: { mode: "sequential" } };
|
|
1295
|
-
const host = speechHost.get(partId);
|
|
1296
|
-
if (host != null) return {
|
|
1297
|
-
timePosition: {
|
|
1298
|
-
mode: "anchored",
|
|
1299
|
-
anchorPartId: host.hostPartId,
|
|
1300
|
-
offsetMs: host.offsetMs
|
|
1301
|
-
},
|
|
1302
|
-
fallbackAbsMs: abs
|
|
1303
|
-
};
|
|
1304
|
-
const part = partLibrary[partId];
|
|
1305
|
-
const captionPart = typeof part === "object" && part != null ? part.caption : void 0;
|
|
1306
|
-
const speechId = captionPart?.speech_part_id ?? void 0;
|
|
1307
|
-
if (speechId != null) return {
|
|
1308
|
-
timePosition: {
|
|
1309
|
-
mode: "anchored",
|
|
1310
|
-
anchorPartId: speechId,
|
|
1311
|
-
offsetMs: Math.round(captionPart?.start_ms ?? 0)
|
|
1312
|
-
},
|
|
1313
|
-
fallbackAbsMs: abs
|
|
1314
|
-
};
|
|
1315
|
-
return { timePosition: {
|
|
1316
|
-
mode: "absolute",
|
|
1317
|
-
offsetMs: abs
|
|
1318
|
-
} };
|
|
1319
|
-
}
|
|
1320
|
-
/** Derive one item's authoritative `time_position` from its legacy abs + aggregation. */
|
|
1321
|
-
function deriveItem(item, isMain, speechHost, partLibrary) {
|
|
1322
|
-
const partId = item.part_id ?? "";
|
|
1323
|
-
const { timePosition, fallbackAbsMs } = derivePositionFromAbs(partId, Math.round(item.abs_time_position ?? 0), isMain, speechHost, partLibrary);
|
|
1324
|
-
return fallbackAbsMs == null ? {
|
|
1325
|
-
part_id: partId,
|
|
1326
|
-
time_position: timePosition
|
|
1327
|
-
} : {
|
|
1328
|
-
part_id: partId,
|
|
1329
|
-
time_position: timePosition,
|
|
1330
|
-
fallback_abs_ms: fallbackAbsMs
|
|
1331
|
-
};
|
|
1332
|
-
}
|
|
1333
|
-
/**
|
|
1334
|
-
* Project the authoritative `VideoDocument` back into the legacy `VideoDraft`
|
|
1335
|
-
* read-view, solving each item's absolute position, the `part_aggregations`, and
|
|
1336
|
-
* the total duration via the timeline-core cascade.
|
|
1337
|
-
*/
|
|
1338
|
-
function fromVideoDocument(document) {
|
|
1339
|
-
assertValidVideoDocument(document);
|
|
1340
|
-
const view = solveVideoDocument(document);
|
|
1341
|
-
const tracks = document.tracks ?? [];
|
|
1342
|
-
const mainTrack = tracks.find((t) => t.parts_kind === "video_clip");
|
|
1343
|
-
const aboveTracks = tracks.filter((t) => t.parts_kind === "caption");
|
|
1344
|
-
const belowTracks = tracks.filter((t) => t.parts_kind === "speech" || t.parts_kind === "bgm");
|
|
1345
|
-
return {
|
|
1346
|
-
id: document.meta.draft_id,
|
|
1347
|
-
project_id: document.meta.project_id,
|
|
1348
|
-
owner_id: document.meta.owner_id,
|
|
1349
|
-
thumbnail_storage_key: document.meta.thumbnail_storage_key,
|
|
1350
|
-
timeline: {
|
|
1351
|
-
duration_ms: view.durationMs,
|
|
1352
|
-
unit_time_ms: document.timeline?.unit_time_ms
|
|
1353
|
-
},
|
|
1354
|
-
video_creation_settings: clone(document.meta.video_creation_settings),
|
|
1355
|
-
chat_session_id: document.meta.chat_session_id,
|
|
1356
|
-
main_track: draftTrack(mainTrack, view.absByPartId),
|
|
1357
|
-
above_main_tracks: aboveTracks.map((t) => draftTrack(t, view.absByPartId)),
|
|
1358
|
-
below_main_tracks: belowTracks.map((t) => draftTrack(t, view.absByPartId)),
|
|
1359
|
-
part_aggregations: view.aggregations,
|
|
1360
|
-
part_library: toReadViewPartLibrary(view.partLibrary, view.durationMs),
|
|
1361
|
-
version: document.meta.version
|
|
1362
|
-
};
|
|
1363
|
-
}
|
|
1364
|
-
/**
|
|
1365
|
-
* Map the authoritative part library into the `VideoDraft` read-view shape,
|
|
1366
|
-
* re-injecting the derived effective `duration_ms` downstream expects
|
|
1367
|
-
* (reference/17 §5/§7) — the authoritative parts store no part-level duration:
|
|
1368
|
-
*
|
|
1369
|
-
* - video clip → `(play_out - play_in) / speed` (`effectiveVideoClipDurationMs`)
|
|
1370
|
-
* - speech → its intrinsic `media_duration_ms` (no trim/speed)
|
|
1371
|
-
* - caption → its generation-time `initial_duration_ms`
|
|
1372
|
-
* - bgm → the timeline total (`durationMs`)
|
|
1373
|
-
*
|
|
1374
|
-
* Parts are deep-cloned; the engine extensions (`media_duration_ms`,
|
|
1375
|
-
* `initial_duration_ms`) are kept alongside the injected `duration_ms`.
|
|
1376
|
-
*/
|
|
1377
|
-
function toReadViewPartLibrary(partLibrary, durationMs) {
|
|
1378
|
-
const out = {};
|
|
1379
|
-
for (const [partId, part] of Object.entries(partLibrary)) if (part.video_clip != null) {
|
|
1380
|
-
const clip = clone(part.video_clip);
|
|
1381
|
-
out[partId] = { video_clip: {
|
|
1382
|
-
...clip,
|
|
1383
|
-
duration_ms: effectiveVideoClipDurationMs(clip)
|
|
1384
|
-
} };
|
|
1385
|
-
} else if (part.speech != null) {
|
|
1386
|
-
const speech = clone(part.speech);
|
|
1387
|
-
out[partId] = { speech: {
|
|
1388
|
-
...speech,
|
|
1389
|
-
duration_ms: speech.media_duration_ms
|
|
1390
|
-
} };
|
|
1391
|
-
} else if (part.caption != null) {
|
|
1392
|
-
const caption = clone(part.caption);
|
|
1393
|
-
out[partId] = { caption: {
|
|
1394
|
-
...caption,
|
|
1395
|
-
duration_ms: caption.initial_duration_ms
|
|
1396
|
-
} };
|
|
1397
|
-
} else if (part.bgm != null) out[partId] = { bgm: {
|
|
1398
|
-
...clone(part.bgm),
|
|
1399
|
-
duration_ms: durationMs
|
|
1400
|
-
} };
|
|
1401
|
-
return out;
|
|
1402
|
-
}
|
|
1403
|
-
/** Deep-clone a part payload, dropping the derived read-view `duration_ms`. */
|
|
1404
|
-
function omitDurationMs(part) {
|
|
1405
|
-
const { duration_ms: _drop, ...rest } = clone(part);
|
|
1406
|
-
return rest;
|
|
1407
|
-
}
|
|
1408
|
-
/**
|
|
1409
|
-
* Migrate a legacy video clip into the authoritative trim-window-only shape: the
|
|
1410
|
-
* authoritative part stores no `duration_ms`, so it is dropped — but a legacy
|
|
1411
|
-
* clip with no explicit trim window (`play_in` / `play_out` absent) would then
|
|
1412
|
-
* compute an effective length of 0 and vanish from the layout. When that clip
|
|
1413
|
-
* carried a legacy `duration_ms`, backfill it as the trim window (`play_in` 0,
|
|
1414
|
-
* `play_out = duration_ms`, no speed) so the effective length is preserved, then
|
|
1415
|
-
* drop `duration_ms`. A clip that already has a trim window keeps it verbatim.
|
|
1416
|
-
*/
|
|
1417
|
-
function withTrimWindowFromDuration(clip) {
|
|
1418
|
-
const stripped = omitDurationMs(clip);
|
|
1419
|
-
if (clip.play_in != null || clip.play_out != null) return stripped;
|
|
1420
|
-
const legacyDuration = clip.duration_ms;
|
|
1421
|
-
if (legacyDuration == null || !Number.isFinite(legacyDuration)) return stripped;
|
|
1422
|
-
return {
|
|
1423
|
-
...stripped,
|
|
1424
|
-
play_in: 0,
|
|
1425
|
-
play_out: legacyDuration
|
|
1426
|
-
};
|
|
1427
|
-
}
|
|
1428
|
-
/** Deep-clone a part payload, returning its `duration_ms` separately from the rest. */
|
|
1429
|
-
function splitDurationMs(part) {
|
|
1430
|
-
const { duration_ms, ...rest } = clone(part);
|
|
1431
|
-
return {
|
|
1432
|
-
duration_ms,
|
|
1433
|
-
rest
|
|
1434
|
-
};
|
|
1435
|
-
}
|
|
1436
|
-
function draftTrack(track, absByPartId) {
|
|
1437
|
-
if (track == null) return void 0;
|
|
1438
|
-
return {
|
|
1439
|
-
id: track.id,
|
|
1440
|
-
parts_kind: track.parts_kind,
|
|
1441
|
-
is_hidden: track.is_hidden,
|
|
1442
|
-
items: (track.items ?? []).map((item) => ({
|
|
1443
|
-
part_id: item.part_id,
|
|
1444
|
-
abs_time_position: absByPartId.get(item.part_id) ?? 0
|
|
1445
|
-
}))
|
|
1446
|
-
};
|
|
1447
|
-
}
|
|
1448
|
-
function clone(value) {
|
|
1449
|
-
if (Array.isArray(value)) return value.map((item) => clone(item));
|
|
1450
|
-
if (value != null && typeof value === "object") {
|
|
1451
|
-
const result = {};
|
|
1452
|
-
for (const [key, child] of Object.entries(value)) result[key] = clone(child);
|
|
1453
|
-
return result;
|
|
1454
|
-
}
|
|
1455
|
-
return value;
|
|
1456
|
-
}
|
|
1457
|
-
//#endregion
|
|
1458
|
-
//#region src/document/mirror-read.ts
|
|
1459
|
-
/**
|
|
1460
|
-
* Project the mirror state (`VideoDocumentDraft`) into the authoritative
|
|
1461
|
-
* `VideoDocument`. The storage shape is isomorphic to the domain shape (RFC 03
|
|
1462
|
-
* §4, reference/17 §4: meta map + a single `tracks` list + part_library), so this
|
|
1463
|
-
* is a near-identity — it reads `time_position` / `fallback_abs_ms` JSON blobs
|
|
1464
|
-
* back into structured values and trims empty strings, nothing more.
|
|
1465
|
-
*
|
|
1466
|
-
* It maps only authoritative facts (RFC 02 §6): `part_id` + `time_position`. The
|
|
1467
|
-
* projection-derived `VideoDraft` read-view (absolute time, `part_aggregations`,
|
|
1468
|
-
* total duration) is solved separately by the cascade, not here.
|
|
1469
|
-
*
|
|
1470
|
-
* It reads in-memory mirror state (O(n) over the document), not a Loro
|
|
1471
|
-
* `toJSON()` FFI rebuild — the cost the prior schema adapter paid on every
|
|
1472
|
-
* `snapshot()`.
|
|
1473
|
-
*/
|
|
1474
|
-
function readVideoDocumentFromDraft(draft) {
|
|
1475
|
-
const partLibrary = {};
|
|
1476
|
-
for (const [partId, payload] of recordEntries(draft.part_library)) {
|
|
1477
|
-
const part = draftToPartUnion(payload);
|
|
1478
|
-
if (part != null) partLibrary[partId] = part;
|
|
1479
|
-
}
|
|
1480
|
-
const timeline = draft.timeline;
|
|
1481
|
-
const hasTimeline = timeline?.unit_time_ms != null;
|
|
1482
|
-
const meta = draft.meta;
|
|
1483
|
-
return {
|
|
1484
|
-
meta: {
|
|
1485
|
-
schema_version: VIDEO_DOCUMENT_SCHEMA_VERSION,
|
|
1486
|
-
draft_id: emptyToUndefined(meta?.draft_id),
|
|
1487
|
-
project_id: emptyToUndefined(meta?.project_id),
|
|
1488
|
-
owner_id: emptyToUndefined(meta?.owner_id),
|
|
1489
|
-
thumbnail_storage_key: emptyToUndefined(meta?.thumbnail_storage_key),
|
|
1490
|
-
chat_session_id: emptyToUndefined(meta?.chat_session_id),
|
|
1491
|
-
video_creation_settings: meta?.video_creation_settings ?? void 0,
|
|
1492
|
-
version: meta?.version ?? void 0
|
|
1493
|
-
},
|
|
1494
|
-
timeline: hasTimeline ? { unit_time_ms: timeline.unit_time_ms } : void 0,
|
|
1495
|
-
tracks: rowsToTracks(draft.tracks),
|
|
1496
|
-
part_library: partLibrary
|
|
1497
|
-
};
|
|
1498
|
-
}
|
|
1499
|
-
/** Map a movable-list of track rows (`$cid`-bearing) into domain tracks, dropping empty-id placeholders. */
|
|
1500
|
-
function rowsToTracks(rows) {
|
|
1501
|
-
return (rows ?? []).filter((row) => row.id != null && row.id !== "").map(rowToTrack);
|
|
1502
|
-
}
|
|
1503
|
-
function rowToTrack(row) {
|
|
1504
|
-
return {
|
|
1505
|
-
id: row.id,
|
|
1506
|
-
parts_kind: row.parts_kind ?? void 0,
|
|
1507
|
-
is_hidden: row.is_hidden ?? void 0,
|
|
1508
|
-
items: (row.items ?? []).map((item) => {
|
|
1509
|
-
const result = {
|
|
1510
|
-
part_id: item.part_id ?? "",
|
|
1511
|
-
time_position: item.time_position
|
|
1512
|
-
};
|
|
1513
|
-
if (item.fallback_abs_ms != null) result.fallback_abs_ms = item.fallback_abs_ms;
|
|
1514
|
-
return result;
|
|
1515
|
-
})
|
|
1516
|
-
};
|
|
1517
|
-
}
|
|
1518
|
-
function emptyToUndefined(value) {
|
|
1519
|
-
return value == null || value === "" ? void 0 : value;
|
|
1520
|
-
}
|
|
1521
|
-
//#endregion
|
|
1522
|
-
//#region src/document/mirror-adapter.ts
|
|
1523
|
-
/**
|
|
1524
|
-
* Storage-layer adapter that backs a `VideoDocument` with `loro-mirror` (ADR
|
|
1525
|
-
* 0008). The mirror holds an in-memory immutable state synced to the `LoroDoc`
|
|
1526
|
-
* by declarative diff, replacing the hand-rolled `@mengine/schema` adapter:
|
|
1527
|
-
*
|
|
1528
|
-
* - `snapshot()` projects the mirror's in-memory state to the read model — no
|
|
1529
|
-
* per-call `toJSON()` FFI rebuild.
|
|
1530
|
-
* - `transact(edit, audit)` runs the whole op in one `mirror.setState` callback:
|
|
1531
|
-
* one diff, one `doc.commit` carrying the audit message. The callback edits an
|
|
1532
|
-
* immer draft, so a throw inside it discards the draft and never touches Loro
|
|
1533
|
-
* (natural rollback) — no `guard` / `rollback` / `openTransaction` machinery.
|
|
1534
|
-
* - mirror's `idSelector` (track items keyed by `part_id`) diffs reorders to
|
|
1535
|
-
* real Loro `move` ops on every lane, so per-item CRDT identity survives on
|
|
1536
|
-
* main and secondary tracks alike.
|
|
1537
|
-
*/
|
|
1538
|
-
var MirrorVideoDocumentAdapter = class {
|
|
1539
|
-
doc;
|
|
1540
|
-
mirror;
|
|
1541
|
-
constructor(doc) {
|
|
1542
|
-
this.doc = doc;
|
|
1543
|
-
this.mirror = new Mirror({
|
|
1544
|
-
doc,
|
|
1545
|
-
schema: videoDocumentMirrorSchema
|
|
1546
|
-
});
|
|
1547
|
-
}
|
|
1548
|
-
snapshot() {
|
|
1549
|
-
return readVideoDocumentFromDraft(this.mirror.getState());
|
|
1550
|
-
}
|
|
1551
|
-
/**
|
|
1552
|
-
* True once the doc holds real document content. A fresh mirror over an empty
|
|
1553
|
-
* doc still reports defaulted root maps, so probe the stored `schema_version`
|
|
1554
|
-
* (empty until a snapshot is bootstrapped or synced in).
|
|
1555
|
-
*/
|
|
1556
|
-
hasContent() {
|
|
1557
|
-
return (this.mirror.getState().meta?.schema_version ?? "") !== "";
|
|
1558
|
-
}
|
|
1559
|
-
/**
|
|
1560
|
-
* Apply one op as a single transaction. `edit` mutates the immer draft; mirror
|
|
1561
|
-
* diffs the result and commits once with the audit `message`. A throw in `edit`
|
|
1562
|
-
* discards the draft (Loro untouched). When `edit` produces no change, mirror
|
|
1563
|
-
* skips the commit — matching the prior "empty op leaves no audit" behavior.
|
|
1564
|
-
*/
|
|
1565
|
-
transact(edit, audit) {
|
|
1566
|
-
this.mirror.setState((draft) => {
|
|
1567
|
-
edit(draft);
|
|
1568
|
-
}, {
|
|
1569
|
-
origin: "mengine.semantic_editor",
|
|
1570
|
-
message: JSON.stringify({
|
|
1571
|
-
semantic_op: audit.kind,
|
|
1572
|
-
payload: audit.payload,
|
|
1573
|
-
intent: audit.intent ?? null
|
|
1574
|
-
})
|
|
1575
|
-
});
|
|
1576
|
-
}
|
|
1577
|
-
};
|
|
1578
|
-
/** Build a fresh Loro doc seeded with `document` through the mirror. */
|
|
1579
|
-
function createMirrorVideoDocument(document, options = {}) {
|
|
1580
|
-
assertValidVideoDocument(document);
|
|
1581
|
-
const doc = new LoroDoc();
|
|
1582
|
-
if (options.peerId != null) doc.setPeerId(options.peerId);
|
|
1583
|
-
new Mirror({
|
|
1584
|
-
doc,
|
|
1585
|
-
schema: videoDocumentMirrorSchema
|
|
1586
|
-
}).setState((draft) => {
|
|
1587
|
-
seedDraft(draft, document);
|
|
1588
|
-
}, { origin: options.origin ?? "mengine.bootstrap" });
|
|
1589
|
-
return doc;
|
|
1590
|
-
}
|
|
1591
|
-
/** Build a `MirrorVideoDocumentAdapter` over a fresh doc seeded with `document`. */
|
|
1592
|
-
function createMirrorVideoDocumentAdapter(document, options) {
|
|
1593
|
-
return new MirrorVideoDocumentAdapter(createMirrorVideoDocument(document, options));
|
|
1594
|
-
}
|
|
1595
|
-
/** Write a whole `VideoDocument` into the mirror draft (used to seed a fresh doc). */
|
|
1596
|
-
function seedDraft(draft, document) {
|
|
1597
|
-
draft.meta = {
|
|
1598
|
-
schema_version: document.meta.schema_version,
|
|
1599
|
-
draft_id: document.meta.draft_id ?? void 0,
|
|
1600
|
-
project_id: document.meta.project_id ?? void 0,
|
|
1601
|
-
owner_id: document.meta.owner_id ?? void 0,
|
|
1602
|
-
thumbnail_storage_key: document.meta.thumbnail_storage_key ?? void 0,
|
|
1603
|
-
chat_session_id: document.meta.chat_session_id ?? void 0,
|
|
1604
|
-
video_creation_settings: document.meta.video_creation_settings ?? void 0,
|
|
1605
|
-
version: document.meta.version ?? void 0
|
|
1606
|
-
};
|
|
1607
|
-
draft.timeline = { unit_time_ms: document.timeline?.unit_time_ms ?? void 0 };
|
|
1608
|
-
draft.tracks = (document.tracks ?? []).map(toTrackRow);
|
|
1609
|
-
draft.part_library = {};
|
|
1610
|
-
for (const [partId, part] of Object.entries(document.part_library ?? {})) draft.part_library[partId] = partUnionToDraft(part);
|
|
1611
|
-
}
|
|
1612
|
-
/** Map a domain `Track` to a draft track row (no lane/lane_order — see schema). */
|
|
1613
|
-
function toTrackRow(track) {
|
|
1614
|
-
return {
|
|
1615
|
-
id: track.id ?? "",
|
|
1616
|
-
parts_kind: track.parts_kind ?? void 0,
|
|
1617
|
-
is_hidden: track.is_hidden ?? void 0,
|
|
1618
|
-
items: (track.items ?? []).map((item) => ({
|
|
1619
|
-
part_id: item.part_id,
|
|
1620
|
-
time_position: item.time_position,
|
|
1621
|
-
fallback_abs_ms: item.fallback_abs_ms
|
|
1622
|
-
}))
|
|
1623
|
-
};
|
|
1624
|
-
}
|
|
1625
|
-
//#endregion
|
|
1626
|
-
//#region src/editor/id-gen.ts
|
|
1627
|
-
/**
|
|
1628
|
-
* Part-id generation, aligned with the online ecosystem.
|
|
1629
|
-
*
|
|
1630
|
-
* The authoritative online producers — agent-harness (`@harness/shared`
|
|
1631
|
-
* `genObjId`) and director.v2 (`common/obj_id.py` `gen_obj_id`) — both mint part
|
|
1632
|
-
* ids as `` `${prefix}_${ulid()}` ``, and real captured drafts use exactly that
|
|
1633
|
-
* shape (`clip_…` / `spe_…` / `cap_…` / `bgm_…`, each a 26-char ULID). The engine
|
|
1634
|
-
* previously emitted `vc_<base36 timestamp><6 random>`, a different prefix AND a
|
|
1635
|
-
* different encoding — the sole cross-repo id divergence. This module removes it
|
|
1636
|
-
* by emitting the same `<prefix>_<ULID>` bytes.
|
|
1637
|
-
*
|
|
1638
|
-
* The ULID is generated inline (Crockford Base32, 48-bit time + 80-bit random)
|
|
1639
|
-
* rather than pulling the `ulid` npm package: the randomness class matches the
|
|
1640
|
-
* old generator (both `Math.random`-based) and it keeps `@mengine/medeo-client`
|
|
1641
|
-
* dependency-free for a purely mechanical id string. Part ids only need to be
|
|
1642
|
-
* unique and lexicographically time-sortable, which this satisfies.
|
|
1643
|
-
*/
|
|
1644
|
-
/** Crockford Base32 alphabet (no I, L, O, U), per the ULID spec. */
|
|
1645
|
-
const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
1646
|
-
const TIME_LEN = 10;
|
|
1647
|
-
const RANDOM_LEN = 16;
|
|
1648
|
-
function encodeTime(now) {
|
|
1649
|
-
let out = "";
|
|
1650
|
-
let ms = now;
|
|
1651
|
-
for (let i = TIME_LEN - 1; i >= 0; i--) {
|
|
1652
|
-
const mod = ms % 32;
|
|
1653
|
-
out = CROCKFORD[mod] + out;
|
|
1654
|
-
ms = (ms - mod) / 32;
|
|
1655
|
-
}
|
|
1656
|
-
return out;
|
|
1657
|
-
}
|
|
1658
|
-
function encodeRandom() {
|
|
1659
|
-
let out = "";
|
|
1660
|
-
for (let i = 0; i < RANDOM_LEN; i++) out += CROCKFORD[Math.floor(Math.random() * 32)];
|
|
1661
|
-
return out;
|
|
1662
|
-
}
|
|
1663
|
-
/** A 26-char Crockford Base32 ULID (10-char time + 16-char random). */
|
|
1664
|
-
function ulid() {
|
|
1665
|
-
return encodeTime(Date.now()) + encodeRandom();
|
|
1666
|
-
}
|
|
1667
|
-
function generatePartId(prefix) {
|
|
1668
|
-
return `${prefix}_${ulid()}`;
|
|
1669
|
-
}
|
|
1670
|
-
//#endregion
|
|
1671
|
-
//#region src/editor/schemas/shared.ts
|
|
1672
|
-
const clipIdSchema = z.string().min(1).describe("The clip part ID on the timeline");
|
|
1673
|
-
const clipIdsSchema = z.array(clipIdSchema).min(1).refine((ids) => new Set(ids).size === ids.length, { message: "Duplicate clip IDs are not allowed" }).describe("List of clip part IDs (no duplicates allowed)");
|
|
1674
|
-
const mediaIdSchema = z.string().min(1).describe("The media asset ID");
|
|
1675
|
-
const speechIdSchema = z.string().min(1).describe("The speech part ID on the timeline");
|
|
1676
|
-
const timelineMsSchema = z.number().int().min(0).describe("Time position in milliseconds on the timeline (>= 0)");
|
|
1677
|
-
const positiveMsSchema = z.number().int().positive().describe("Duration in milliseconds (> 0)");
|
|
1678
|
-
const volumeSchema = z.number().min(-60).max(20).describe("Volume in decibels (-60.0 to 20.0; 0.0 = original, -60 = mute, +20 = max)");
|
|
1679
|
-
const speechIdsSchema = z.array(speechIdSchema).min(1).refine((ids) => new Set(ids).size === ids.length, { message: "Duplicate speech IDs are not allowed" }).describe("List of speech part IDs (no duplicates allowed)");
|
|
1680
|
-
const tangentHandleSchema = z.object({
|
|
1681
|
-
x: z.number().finite(),
|
|
1682
|
-
y: z.number().finite()
|
|
1683
|
-
}).describe("Bezier tangent handle (x, y)");
|
|
1684
|
-
const speedKeyframeSchema = z.object({
|
|
1685
|
-
position: z.number().min(0).max(1),
|
|
1686
|
-
rate: z.number().min(0),
|
|
1687
|
-
in_tangent: tangentHandleSchema.optional(),
|
|
1688
|
-
out_tangent: tangentHandleSchema.optional()
|
|
1689
|
-
}).describe("A speed keyframe: normalized position (0..1), rate, optional tangents");
|
|
1690
|
-
/**
|
|
1691
|
-
* A clip's playback-speed fact, the only thing `SetVideoClipSpeedShift` writes.
|
|
1692
|
-
* Mirrors the IDL `SpeedShift`: `category` is `linear` | `curve`, and `config`
|
|
1693
|
-
* is a discriminated union — `{ linear: { speed } }` for a constant multiplier
|
|
1694
|
-
* (the multiplier projection reads at `config.linear.speed`) or `{ curve: {
|
|
1695
|
-
* keyframes } }` for a Bezier-controlled variable speed (RFC 02 / `reference/16`
|
|
1696
|
-
* §0). Exactly one of `linear` / `curve` is present.
|
|
1697
|
-
*/
|
|
1698
|
-
const speedShiftSchema = z.object({
|
|
1699
|
-
category: z.enum(["linear", "curve"]),
|
|
1700
|
-
mode: z.string(),
|
|
1701
|
-
config: z.union([z.object({ linear: z.object({ speed: z.number().finite().positive() }) }), z.object({ curve: z.object({ keyframes: z.array(speedKeyframeSchema).min(2) }) })])
|
|
1702
|
-
}).describe("Speed shift: linear multiplier or Bezier curve, mirroring the IDL shape");
|
|
1703
|
-
const voiceSchema = z.object({
|
|
1704
|
-
id: z.string().min(1),
|
|
1705
|
-
name: z.string()
|
|
1706
|
-
}).describe("TTS voice summary attached to a speech");
|
|
1707
|
-
//#endregion
|
|
1708
|
-
//#region src/editor/schemas/speech-assets.ts
|
|
1709
|
-
/**
|
|
1710
|
-
* The materialized TTS result shared by `AddSpeeches` / `ChangeSpeechScript` /
|
|
1711
|
-
* `ChangeSpeechVoice` (see `results/phase-4-side-effect-payload-contract.md`
|
|
1712
|
-
* §1/§2). The side effect (TTS/ASR + billing) runs upstream; the op receives the
|
|
1713
|
-
* stable speech + caption parts and writes them as authoritative facts. No
|
|
1714
|
-
* cascade runs on write — the projection derives absolute positions on read.
|
|
1715
|
-
*
|
|
1716
|
-
* Each speech carries the anchoring fact directly (RFC 02 §4): the host video
|
|
1717
|
-
* clip `anchor_part_id` and the `offset_ms` within it. The upstream caller
|
|
1718
|
-
* already knows which clip a speech attaches to, so the op writes
|
|
1719
|
-
* `{ mode:'anchored', anchorPartId, offsetMs }` verbatim — no write-time
|
|
1720
|
-
* host-picking. Captions anchor to their speech via the caption part's
|
|
1721
|
-
* `start_ms` (offset within the speech).
|
|
1722
|
-
*/
|
|
1723
|
-
const speechAssetSchema = z.object({
|
|
1724
|
-
speech_id: speechIdSchema.describe("The speech part ID (= side-effect speech_parts[].id)"),
|
|
1725
|
-
anchor_part_id: clipIdSchema.describe("Host video clip part ID the speech anchors to (RFC 02 §4)"),
|
|
1726
|
-
offset_ms: timelineMsSchema.describe("Offset within the host clip (speech.abs = host.abs + offset_ms)"),
|
|
1727
|
-
audio_storage_key: z.string().min(1),
|
|
1728
|
-
duration_ms: positiveMsSchema,
|
|
1729
|
-
audio_script: z.string(),
|
|
1730
|
-
volume: volumeSchema,
|
|
1731
|
-
voice: voiceSchema,
|
|
1732
|
-
origin_speech_id: z.string().min(1),
|
|
1733
|
-
caption_ids: z.array(z.string().min(1)).describe("Caption part IDs owned by this speech")
|
|
1734
|
-
});
|
|
1735
|
-
const captionAssetSchema = z.object({
|
|
1736
|
-
caption_id: z.string().min(1).describe("The caption part ID (= side-effect created_caption_parts[].id)"),
|
|
1737
|
-
speech_part_id: speechIdSchema.describe("The owning speech part ID"),
|
|
1738
|
-
text: z.string(),
|
|
1739
|
-
start_ms: timelineMsSchema.describe("Offset within the host speech (caption.abs = speech.abs + start_ms)"),
|
|
1740
|
-
duration_ms: positiveMsSchema
|
|
1741
|
-
});
|
|
1742
|
-
/** A materialized speech-subtree write (speeches + their captions). */
|
|
1743
|
-
const speechAssetsSchema = z.object({
|
|
1744
|
-
speeches: z.array(speechAssetSchema).min(1).describe("Materialized speech parts to write"),
|
|
1745
|
-
captions: z.array(captionAssetSchema).describe("Materialized caption parts owned by the speeches")
|
|
1746
|
-
});
|
|
1747
|
-
//#endregion
|
|
1748
|
-
//#region src/editor/schemas/add-speeches.ts
|
|
1749
|
-
/**
|
|
1750
|
-
* Add speeches (and their captions). TTS runs upstream; the stable speech /
|
|
1751
|
-
* caption parts arrive materialized (see `speech-assets.ts`). The op writes the
|
|
1752
|
-
* parts and each speech's `{ mode:'anchored', anchorPartId, offsetMs }` fact
|
|
1753
|
-
* verbatim — no write-time host-picking, no cascade (RFC 02 §4). The projection
|
|
1754
|
-
* derives absolute positions on read.
|
|
1755
|
-
*/
|
|
1756
|
-
const addSpeechesInputSchema = speechAssetsSchema.describe("Materialized speeches + captions to add");
|
|
1757
|
-
//#endregion
|
|
1758
|
-
//#region src/editor/schemas/add-video-clips.ts
|
|
1759
|
-
/**
|
|
1760
|
-
* Add video clips to a track. Each clip's duration facts are separated so a
|
|
1761
|
-
* single number is never overloaded (RFC 02 / `reference/16` §0b):
|
|
1762
|
-
*
|
|
1763
|
-
* - `media_duration_ms` is the source media's intrinsic full length (a resource
|
|
1764
|
-
* fact, written to the part);
|
|
1765
|
-
* - `play_in` / `play_out` are the optional trim window into that media; when
|
|
1766
|
-
* omitted the whole media is used (`play_in=0`, `play_out=media_duration_ms`).
|
|
1767
|
-
*
|
|
1768
|
-
* The clip's effective timeline duration is derived by the projection from the
|
|
1769
|
-
* trim window and `speed_shift` — it is never an input here.
|
|
1770
|
-
*/
|
|
1771
|
-
const addVideoClipsInputSchema = z.object({
|
|
1772
|
-
clips: z.array(z.object({
|
|
1773
|
-
media_id: mediaIdSchema.describe("The media asset ID for the video clip"),
|
|
1774
|
-
start_ms: timelineMsSchema.optional().describe("Absolute start time in milliseconds on the timeline"),
|
|
1775
|
-
media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
|
|
1776
|
-
play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
|
|
1777
|
-
play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)"),
|
|
1778
|
-
track_id: z.string().min(1).optional().describe("Target track ID (optional, defaults to main track)")
|
|
1779
|
-
})).min(1).describe("List of video clips to create"),
|
|
1780
|
-
before_clip_id: z.string().min(1).optional().describe("Insert new clips before this clip ID"),
|
|
1781
|
-
after_clip_id: z.string().min(1).optional().describe("Insert new clips after this clip ID")
|
|
1782
|
-
}).superRefine((data, ctx) => {
|
|
1783
|
-
if (data.before_clip_id != null && data.after_clip_id != null) {
|
|
1784
|
-
ctx.addIssue({
|
|
1785
|
-
code: z.ZodIssueCode.custom,
|
|
1786
|
-
message: "Cannot provide both before_clip_id and after_clip_id"
|
|
1787
|
-
});
|
|
1788
|
-
return;
|
|
1789
|
-
}
|
|
1790
|
-
const hasRelative = data.before_clip_id != null || data.after_clip_id != null;
|
|
1791
|
-
for (let i = 0; i < data.clips.length; i++) {
|
|
1792
|
-
const clip = data.clips[i];
|
|
1793
|
-
if (hasRelative && clip.start_ms != null) ctx.addIssue({
|
|
1794
|
-
code: z.ZodIssueCode.custom,
|
|
1795
|
-
message: `clips[${i}].start_ms must not be provided when using before_clip_id or after_clip_id`,
|
|
1796
|
-
path: [
|
|
1797
|
-
"clips",
|
|
1798
|
-
i,
|
|
1799
|
-
"start_ms"
|
|
1800
|
-
]
|
|
1801
|
-
});
|
|
1802
|
-
if (!hasRelative && clip.start_ms == null) ctx.addIssue({
|
|
1803
|
-
code: z.ZodIssueCode.custom,
|
|
1804
|
-
message: `clips[${i}].start_ms is required when not using relative positioning`,
|
|
1805
|
-
path: [
|
|
1806
|
-
"clips",
|
|
1807
|
-
i,
|
|
1808
|
-
"start_ms"
|
|
1809
|
-
]
|
|
1810
|
-
});
|
|
1811
|
-
const playIn = clip.play_in ?? 0;
|
|
1812
|
-
const playOut = clip.play_out ?? clip.media_duration_ms;
|
|
1813
|
-
if (playOut > clip.media_duration_ms) ctx.addIssue({
|
|
1814
|
-
code: z.ZodIssueCode.custom,
|
|
1815
|
-
message: `clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
|
|
1816
|
-
path: [
|
|
1817
|
-
"clips",
|
|
1818
|
-
i,
|
|
1819
|
-
"play_out"
|
|
1820
|
-
]
|
|
1821
|
-
});
|
|
1822
|
-
if (playIn >= playOut) ctx.addIssue({
|
|
1823
|
-
code: z.ZodIssueCode.custom,
|
|
1824
|
-
message: `clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
|
|
1825
|
-
path: [
|
|
1826
|
-
"clips",
|
|
1827
|
-
i,
|
|
1828
|
-
"play_in"
|
|
1829
|
-
]
|
|
1830
|
-
});
|
|
1831
|
-
}
|
|
1832
|
-
});
|
|
1833
|
-
//#endregion
|
|
1834
|
-
//#region src/editor/schemas/adjust-bgm-volume.ts
|
|
1835
|
-
const adjustBgmVolumeInputSchema = z.object({ bgm: z.array(z.object({
|
|
1836
|
-
bgm_id: clipIdSchema.describe("The bgm part ID to adjust volume for"),
|
|
1837
|
-
volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
|
|
1838
|
-
})).min(1).describe("List of bgm parts with their new volume settings") });
|
|
1839
|
-
//#endregion
|
|
1840
|
-
//#region src/editor/schemas/adjust-speech-volume.ts
|
|
1841
|
-
const adjustSpeechVolumeInputSchema = z.object({ speeches: z.array(z.object({
|
|
1842
|
-
speech_id: speechIdSchema.describe("The speech part ID to adjust volume for"),
|
|
1843
|
-
volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
|
|
1844
|
-
})).min(1).describe("List of speeches with their new volume settings") });
|
|
1845
|
-
//#endregion
|
|
1846
|
-
//#region src/editor/schemas/adjust-video-clip-duration.ts
|
|
1847
|
-
/**
|
|
1848
|
-
* Re-trim existing video clips (the user-facing "adjust duration" gesture is a
|
|
1849
|
-
* trim of the source window). The new `play_in` / `play_out` are the facts; the
|
|
1850
|
-
* effective timeline duration is derived from them and the clip's `speed_shift`,
|
|
1851
|
-
* and the change reflows downstream clips, speeches, and the timeline inside the
|
|
1852
|
-
* op's transaction (no caller-materialized cascade).
|
|
1853
|
-
*/
|
|
1854
|
-
const adjustVideoClipDurationInputSchema = z.object({ clips: z.array(z.object({
|
|
1855
|
-
clip_id: clipIdSchema.describe("The video clip part ID to re-trim"),
|
|
1856
|
-
play_in: timelineMsSchema.describe("New trim window start in the source media"),
|
|
1857
|
-
play_out: positiveMsSchema.describe("New trim window end in the source media")
|
|
1858
|
-
})).min(1).describe("Video clips with their new trim windows") });
|
|
1859
|
-
//#endregion
|
|
1860
|
-
//#region src/editor/schemas/adjust-video-clip-volume.ts
|
|
1861
|
-
const adjustVideoClipVolumeInputSchema = z.object({ clips: z.array(z.object({
|
|
1862
|
-
clip_id: clipIdSchema.describe("The video clip part ID to adjust volume for"),
|
|
1863
|
-
volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
|
|
1864
|
-
})).min(1).describe("List of video clips with their new volume settings") });
|
|
1865
|
-
//#endregion
|
|
1866
|
-
//#region src/editor/schemas/change-speech.ts
|
|
1867
|
-
/**
|
|
1868
|
-
* Change a speech's script or voice. Both re-run TTS upstream and return the
|
|
1869
|
-
* regenerated speech / caption parts in the same materialized shape as
|
|
1870
|
-
* `AddSpeeches` (`speech-assets.ts`); the op upserts them by id (the speech part
|
|
1871
|
-
* id is preserved across a re-TTS), re-seats at `start_ms`, and reflows. Old
|
|
1872
|
-
* caption parts no longer owned by the speech are removed via `caption_ids`.
|
|
1873
|
-
*/
|
|
1874
|
-
const changeSpeechScriptInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new script)");
|
|
1875
|
-
const changeSpeechVoiceInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new voice)");
|
|
1876
|
-
//#endregion
|
|
1877
|
-
//#region src/editor/schemas/delete-bgm.ts
|
|
1878
|
-
/**
|
|
1879
|
-
* Remove the document BGM. Pure document edit: clears the bgm lane and removes
|
|
1880
|
-
* the bgm part. Takes no input (a document holds at most one bgm); an empty
|
|
1881
|
-
* object keeps the op signature uniform with the rest.
|
|
1882
|
-
*/
|
|
1883
|
-
const deleteBgmInputSchema = z.object({}).describe("Remove the document BGM (no parameters)");
|
|
1884
|
-
//#endregion
|
|
1885
|
-
//#region src/editor/schemas/delete-speeches.ts
|
|
1886
|
-
/**
|
|
1887
|
-
* Delete speeches with their captions. Pure document edit (no side effect): the
|
|
1888
|
-
* op removes each speech part, cascade-deletes the captions it owns (via
|
|
1889
|
-
* `caption_ids` / `speech_part_id`), drops their track items, and reflows.
|
|
1890
|
-
*/
|
|
1891
|
-
const deleteSpeechesInputSchema = z.object({ speech_ids: speechIdsSchema.describe("Speech part IDs to delete (their captions cascade-delete)") });
|
|
1892
|
-
//#endregion
|
|
1893
|
-
//#region src/editor/schemas/delete-video-clips.ts
|
|
1894
|
-
/**
|
|
1895
|
-
* How a delete handles the anchored subtree (speeches anchored to a deleted clip,
|
|
1896
|
-
* and their captions) — a delete-op policy, not a data-model field (reference/17
|
|
1897
|
-
* §6). `cascade` (default) removes the subtree; `detach` keeps the direct
|
|
1898
|
-
* anchored children, re-pinning them to `absolute` so they stay on the timeline.
|
|
1899
|
-
*/
|
|
1900
|
-
const anchoredDeletePolicySchema = z.enum(["cascade", "detach"]);
|
|
1901
|
-
const deleteVideoClipsInputSchema = z.object({
|
|
1902
|
-
clip_ids: clipIdsSchema.describe("List of video clip part IDs to delete from the main track"),
|
|
1903
|
-
on_anchored: anchoredDeletePolicySchema.optional().describe("How to treat anchored children (default cascade)")
|
|
1904
|
-
});
|
|
1905
|
-
//#endregion
|
|
1906
|
-
//#region src/editor/schemas/move-speeches.ts
|
|
1907
|
-
/**
|
|
1908
|
-
* Move speeches in time. Pure document edit: the op re-seats each speech at its
|
|
1909
|
-
* new absolute `start_ms`; the cascade reassigns it to the host video clip,
|
|
1910
|
-
* resolves overlaps, and reflows. Captions follow their speech.
|
|
1911
|
-
*/
|
|
1912
|
-
const moveSpeechesInputSchema = z.object({ speeches: z.array(z.object({
|
|
1913
|
-
speech_id: speechIdSchema.describe("The speech part ID to move"),
|
|
1914
|
-
new_start_ms: timelineMsSchema.describe("New absolute start time on the timeline")
|
|
1915
|
-
})).min(1).describe("Speeches to move to new positions") });
|
|
1916
|
-
//#endregion
|
|
1917
|
-
//#region src/editor/schemas/move-video-clips.ts
|
|
1918
|
-
const moveVideoClipsInputSchema = z.object({ clips: z.array(z.object({
|
|
1919
|
-
clip_id: clipIdSchema.describe("The video clip part ID to move"),
|
|
1920
|
-
new_start_ms: timelineMsSchema.describe("New absolute start time in milliseconds on the timeline"),
|
|
1921
|
-
new_track_id: z.string().min(1).optional().describe("Target track ID to move the clip to (optional)")
|
|
1922
|
-
})).min(1).describe("List of video clips to move to new positions") });
|
|
1923
|
-
//#endregion
|
|
1924
|
-
//#region src/editor/schemas/replace-video-clip-content.ts
|
|
1925
|
-
/**
|
|
1926
|
-
* Replace the media backing existing video clips. The media import runs upstream
|
|
1927
|
-
* (Director); its stable result — the new media id, intrinsic length, and the
|
|
1928
|
-
* reset trim window — arrives materialized (see
|
|
1929
|
-
* `results/phase-4-side-effect-payload-contract.md` §4). Director resets
|
|
1930
|
-
* `play_in=0` / `play_out=media_duration_ms` and clears `speed_shift` on
|
|
1931
|
-
* replacement. The clip `part_id`s (hence their track items) are unchanged; the
|
|
1932
|
-
* editor reflows the main track from the new effective durations.
|
|
1933
|
-
*/
|
|
1934
|
-
const replaceVideoClipContentInputSchema = z.object({ clips: z.array(z.object({
|
|
1935
|
-
clip_id: clipIdSchema.describe("Existing video clip part ID to re-point"),
|
|
1936
|
-
origin_media_id: mediaIdSchema.describe("The new media asset ID"),
|
|
1937
|
-
media_duration_ms: positiveMsSchema.describe("The new media's intrinsic full length"),
|
|
1938
|
-
play_in: timelineMsSchema.describe("Trim window start in the new media (usually 0)"),
|
|
1939
|
-
play_out: positiveMsSchema.describe("Trim window end in the new media (usually = media_duration_ms)"),
|
|
1940
|
-
volume: volumeSchema
|
|
1941
|
-
})).min(1).describe("Video clips whose media is being replaced") });
|
|
1942
|
-
//#endregion
|
|
1943
|
-
//#region src/editor/schemas/set-bgm.ts
|
|
1944
|
-
/**
|
|
1945
|
-
* Set the document BGM. The media's stable result (storage key) arrives
|
|
1946
|
-
* materialized from upstream (see
|
|
1947
|
-
* `results/phase-4-side-effect-payload-contract.md` §3). The op upserts the bgm
|
|
1948
|
-
* part and seats it on the bgm lane; its effective length is always the whole
|
|
1949
|
-
* timeline, derived by the projection on read — so there is no `duration_ms`
|
|
1950
|
-
* input or fact (RFC 02 / `reference/16` §0b). A `bgm_id` lets the op replace an
|
|
1951
|
-
* existing bgm part by id.
|
|
1952
|
-
*/
|
|
1953
|
-
const setBgmInputSchema = z.object({
|
|
1954
|
-
bgm_id: z.string().min(1).describe("The bgm part ID to write"),
|
|
1955
|
-
audio_storage_key: z.string().min(1),
|
|
1956
|
-
origin_media_id: mediaIdSchema,
|
|
1957
|
-
volume: volumeSchema
|
|
1958
|
-
});
|
|
1959
|
-
//#endregion
|
|
1960
|
-
//#region src/editor/schemas/set-caption-style.ts
|
|
1961
|
-
/**
|
|
1962
|
-
* Set the caption visual style. GLOBAL by design: the style applies to every
|
|
1963
|
-
* caption part in the document — it carries NO `caption_id`. This mirrors the FE,
|
|
1964
|
-
* whose caption-style store (`caption-style.ts:persistCaptionStylePatch`) iterates
|
|
1965
|
-
* ALL captions and writes the same normalized style to each; the product has a
|
|
1966
|
-
* single document-wide caption style, not per-caption styling.
|
|
1967
|
-
*
|
|
1968
|
-
* Every field is optional and maps to a `CaptionStyle` attribute (snake_case
|
|
1969
|
-
* IDL). A field present in the input is written to every caption; a field ABSENT
|
|
1970
|
-
* from the input is left untouched on each caption (the editor merges the patch
|
|
1971
|
-
* onto each caption's existing style — this is a value edit, not a full-style
|
|
1972
|
-
* replace, so a partial patch such as "recolor only" does not wipe font size).
|
|
1973
|
-
*
|
|
1974
|
-
* Pure document edit, no cascade — captions keep their positions; only the style
|
|
1975
|
-
* sub-map of each caption part changes.
|
|
1976
|
-
*/
|
|
1977
|
-
const setCaptionStyleInputSchema = z.object({
|
|
1978
|
-
font_id: z.string().min(1).optional().describe("Font ID referencing a font from the font library"),
|
|
1979
|
-
font_size: z.number().positive().optional().describe("Font size in points"),
|
|
1980
|
-
font_color: z.string().min(1).optional().describe("Font color as hex string, e.g. \"#FFFFFF\""),
|
|
1981
|
-
font_weight: z.number().int().optional().describe("Numeric font weight, e.g. 400 or 700"),
|
|
1982
|
-
entrance_animation: z.string().optional().describe("Entrance animation preset ID, e.g. \"fade\" or \"none\""),
|
|
1983
|
-
entrance_animation_duration_ms: z.number().min(0).optional().describe("Entrance animation duration in ms"),
|
|
1984
|
-
stroke_color: z.string().min(1).optional().describe("Outline/stroke color as hex string, e.g. \"#000000\""),
|
|
1985
|
-
stroke_width: z.number().min(0).optional().describe("Outline/stroke width in pixels"),
|
|
1986
|
-
position_x: z.number().optional().describe("Caption center X as a fraction (0.0 to 1.0)"),
|
|
1987
|
-
position_y: z.number().optional().describe("Caption center Y as a fraction (0.0 to 1.0)")
|
|
1988
|
-
}).describe("Document-wide caption style patch (no caption_id; applies to every caption)");
|
|
1989
|
-
//#endregion
|
|
1990
|
-
//#region src/editor/schemas/set-caption-visibility.ts
|
|
1991
|
-
/**
|
|
1992
|
-
* Toggle caption visibility (the caption track's `is_hidden` flag). Pure
|
|
1993
|
-
* document edit, no cascade — captions keep their positions; only the lane's
|
|
1994
|
-
* hidden flag changes.
|
|
1995
|
-
*/
|
|
1996
|
-
const setCaptionVisibilityInputSchema = z.object({ is_hidden: z.boolean().describe("Whether the caption track is hidden") });
|
|
1997
|
-
//#endregion
|
|
1998
|
-
//#region src/editor/schemas/set-video-clip-speed-shift.ts
|
|
1999
|
-
/**
|
|
2000
|
-
* Set the playback speed of existing video clips. Per the speed-shift decision
|
|
2001
|
-
* (`reference/16` §0): the op writes only the `speed_shift` fact — it does NOT
|
|
2002
|
-
* store an effective `duration_ms` (projection derives it from the trim window /
|
|
2003
|
-
* speed) and does NOT scale anchored speeches' relative offsets (offsets stay
|
|
2004
|
-
* put; the cascade reflows absolute positions). A `null` speed_shift clears the
|
|
2005
|
-
* speed back to original (1×).
|
|
2006
|
-
*/
|
|
2007
|
-
const setVideoClipSpeedShiftInputSchema = z.object({ clips: z.array(z.object({
|
|
2008
|
-
clip_id: clipIdSchema.describe("The video clip part ID to set speed for"),
|
|
2009
|
-
speed_shift: speedShiftSchema.nullable().describe("The new speed setting, or null to reset to 1×")
|
|
2010
|
-
})).min(1).describe("Video clips with their new speed settings") });
|
|
2011
|
-
//#endregion
|
|
2012
|
-
//#region src/editor/schemas/index.ts
|
|
2013
|
-
var schemas_exports = /* @__PURE__ */ __exportAll({
|
|
2014
|
-
addSpeechesInputSchema: () => addSpeechesInputSchema,
|
|
2015
|
-
addVideoClipsInputSchema: () => addVideoClipsInputSchema,
|
|
2016
|
-
adjustBgmVolumeInputSchema: () => adjustBgmVolumeInputSchema,
|
|
2017
|
-
adjustSpeechVolumeInputSchema: () => adjustSpeechVolumeInputSchema,
|
|
2018
|
-
adjustVideoClipDurationInputSchema: () => adjustVideoClipDurationInputSchema,
|
|
2019
|
-
adjustVideoClipVolumeInputSchema: () => adjustVideoClipVolumeInputSchema,
|
|
2020
|
-
anchoredDeletePolicySchema: () => anchoredDeletePolicySchema,
|
|
2021
|
-
changeSpeechScriptInputSchema: () => changeSpeechScriptInputSchema,
|
|
2022
|
-
changeSpeechVoiceInputSchema: () => changeSpeechVoiceInputSchema,
|
|
2023
|
-
clipIdSchema: () => clipIdSchema,
|
|
2024
|
-
clipIdsSchema: () => clipIdsSchema,
|
|
2025
|
-
deleteBgmInputSchema: () => deleteBgmInputSchema,
|
|
2026
|
-
deleteSpeechesInputSchema: () => deleteSpeechesInputSchema,
|
|
2027
|
-
deleteVideoClipsInputSchema: () => deleteVideoClipsInputSchema,
|
|
2028
|
-
mediaIdSchema: () => mediaIdSchema,
|
|
2029
|
-
moveSpeechesInputSchema: () => moveSpeechesInputSchema,
|
|
2030
|
-
moveVideoClipsInputSchema: () => moveVideoClipsInputSchema,
|
|
2031
|
-
positiveMsSchema: () => positiveMsSchema,
|
|
2032
|
-
replaceVideoClipContentInputSchema: () => replaceVideoClipContentInputSchema,
|
|
2033
|
-
setBgmInputSchema: () => setBgmInputSchema,
|
|
2034
|
-
setCaptionStyleInputSchema: () => setCaptionStyleInputSchema,
|
|
2035
|
-
setCaptionVisibilityInputSchema: () => setCaptionVisibilityInputSchema,
|
|
2036
|
-
setVideoClipSpeedShiftInputSchema: () => setVideoClipSpeedShiftInputSchema,
|
|
2037
|
-
speechAssetsSchema: () => speechAssetsSchema,
|
|
2038
|
-
speechIdSchema: () => speechIdSchema,
|
|
2039
|
-
speechIdsSchema: () => speechIdsSchema,
|
|
2040
|
-
speedShiftSchema: () => speedShiftSchema,
|
|
2041
|
-
timelineMsSchema: () => timelineMsSchema,
|
|
2042
|
-
voiceSchema: () => voiceSchema,
|
|
2043
|
-
volumeSchema: () => volumeSchema
|
|
2044
|
-
});
|
|
2045
|
-
//#endregion
|
|
2046
159
|
//#region src/editor/snapshot-utils.ts
|
|
2047
160
|
function isMap(value) {
|
|
2048
161
|
return value instanceof Map;
|
|
@@ -2350,9 +463,11 @@ function readPartLibrary(draft) {
|
|
|
2350
463
|
var SemanticEditor = class {
|
|
2351
464
|
doc;
|
|
2352
465
|
validator;
|
|
2353
|
-
|
|
466
|
+
idFactory;
|
|
467
|
+
constructor(doc, validator = new SchemaValidator(), idFactory = generatePartId) {
|
|
2354
468
|
this.doc = doc;
|
|
2355
469
|
this.validator = validator;
|
|
470
|
+
this.idFactory = idFactory;
|
|
2356
471
|
}
|
|
2357
472
|
async moveVideoClips(input, options) {
|
|
2358
473
|
this.validator.validateMoveVideoClips(input, this.doc);
|
|
@@ -2394,7 +509,7 @@ var SemanticEditor = class {
|
|
|
2394
509
|
track.items ??= [];
|
|
2395
510
|
let at = insertIndex;
|
|
2396
511
|
for (const clip of input.clips) {
|
|
2397
|
-
const partId =
|
|
512
|
+
const partId = this.idFactory("clip");
|
|
2398
513
|
setPart(draft, partId, { video_clip: {
|
|
2399
514
|
id: partId,
|
|
2400
515
|
kind: "video_clip",
|
|
@@ -2934,6 +1049,105 @@ function deleteAnchoredSubtree(draft, clipId) {
|
|
|
2934
1049
|
for (const speechId of anchored) deleteSpeechSubtree(draft, speechId);
|
|
2935
1050
|
}
|
|
2936
1051
|
//#endregion
|
|
1052
|
+
//#region src/editor/journal.ts
|
|
1053
|
+
/**
|
|
1054
|
+
* Wire a plain-memory adapter + editor that share a recording id factory, so
|
|
1055
|
+
* every mutating transact lands in `journal` with ordered `generated_ids`.
|
|
1056
|
+
*/
|
|
1057
|
+
function createEditSandbox(document, options) {
|
|
1058
|
+
const adapter = createPlainMemoryAdapter(document, options);
|
|
1059
|
+
return {
|
|
1060
|
+
adapter,
|
|
1061
|
+
editor: new SemanticEditor(adapter, new SchemaValidator(), adapter.idFactory),
|
|
1062
|
+
journal: adapter.journal
|
|
1063
|
+
};
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* Re-drive `doc` from a recorded journal, forcing each entry's `generated_ids`
|
|
1067
|
+
* through a queue-backed id factory. Never mints fresh ids: an empty queue on
|
|
1068
|
+
* demand throws `unrecorded id`; leftover ids after an entry throws
|
|
1069
|
+
* `unconsumed ids`. Legacy entries without `generated_ids` are treated as `[]`.
|
|
1070
|
+
*/
|
|
1071
|
+
async function replayJournal(doc, journal) {
|
|
1072
|
+
const queue = [];
|
|
1073
|
+
const idFactory = (_prefix) => {
|
|
1074
|
+
const id = queue.shift();
|
|
1075
|
+
if (id == null) throw new Error("unrecorded id");
|
|
1076
|
+
return id;
|
|
1077
|
+
};
|
|
1078
|
+
const editor = new SemanticEditor(doc, new SchemaValidator(), idFactory);
|
|
1079
|
+
for (const entry of journal) {
|
|
1080
|
+
queue.push(...entry.generated_ids ?? []);
|
|
1081
|
+
await dispatchEntry(editor, entry);
|
|
1082
|
+
if (queue.length > 0) throw new Error("unconsumed ids");
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
/** Dispatch one journal entry to the matching editor method; payload is passed through. */
|
|
1086
|
+
async function dispatchEntry(editor, entry) {
|
|
1087
|
+
const payload = entry.payload;
|
|
1088
|
+
const options = entry.intent != null ? { intent: entry.intent } : void 0;
|
|
1089
|
+
switch (entry.kind) {
|
|
1090
|
+
case "MoveVideoClips":
|
|
1091
|
+
await editor.moveVideoClips(payload, options);
|
|
1092
|
+
return;
|
|
1093
|
+
case "DeleteVideoClips":
|
|
1094
|
+
await editor.deleteVideoClips(payload, options);
|
|
1095
|
+
return;
|
|
1096
|
+
case "AddVideoClips":
|
|
1097
|
+
await editor.addVideoClips(payload, options);
|
|
1098
|
+
return;
|
|
1099
|
+
case "AdjustVideoClipVolume":
|
|
1100
|
+
await editor.adjustVideoClipVolume(payload, options);
|
|
1101
|
+
return;
|
|
1102
|
+
case "SetVideoClipSpeedShift":
|
|
1103
|
+
await editor.setVideoClipSpeedShift(payload, options);
|
|
1104
|
+
return;
|
|
1105
|
+
case "ReplaceVideoClipContent":
|
|
1106
|
+
await editor.replaceVideoClipContent(payload, options);
|
|
1107
|
+
return;
|
|
1108
|
+
case "AdjustVideoClipDuration":
|
|
1109
|
+
await editor.adjustVideoClipDuration(payload, options);
|
|
1110
|
+
return;
|
|
1111
|
+
case "AddSpeeches":
|
|
1112
|
+
await editor.addSpeeches(payload, options);
|
|
1113
|
+
return;
|
|
1114
|
+
case "DeleteSpeeches":
|
|
1115
|
+
await editor.deleteSpeeches(payload, options);
|
|
1116
|
+
return;
|
|
1117
|
+
case "MoveSpeeches":
|
|
1118
|
+
await editor.moveSpeeches(payload, options);
|
|
1119
|
+
return;
|
|
1120
|
+
case "ChangeSpeechScript":
|
|
1121
|
+
await editor.changeSpeechScript(payload, options);
|
|
1122
|
+
return;
|
|
1123
|
+
case "ChangeSpeechVoice":
|
|
1124
|
+
await editor.changeSpeechVoice(payload, options);
|
|
1125
|
+
return;
|
|
1126
|
+
case "AdjustSpeechVolume":
|
|
1127
|
+
await editor.adjustSpeechVolume(payload, options);
|
|
1128
|
+
return;
|
|
1129
|
+
case "SetCaptionVisibility":
|
|
1130
|
+
await editor.setCaptionVisibility(payload, options);
|
|
1131
|
+
return;
|
|
1132
|
+
case "SetCaptionStyle":
|
|
1133
|
+
await editor.setCaptionStyle(payload, options);
|
|
1134
|
+
return;
|
|
1135
|
+
case "SetBgm":
|
|
1136
|
+
await editor.setBgm(payload, options);
|
|
1137
|
+
return;
|
|
1138
|
+
case "DeleteBgm":
|
|
1139
|
+
await editor.deleteBgm(payload, options);
|
|
1140
|
+
return;
|
|
1141
|
+
case "AdjustBgmVolume":
|
|
1142
|
+
await editor.adjustBgmVolume(payload, options);
|
|
1143
|
+
return;
|
|
1144
|
+
default: {
|
|
1145
|
+
const _exhaustive = entry.kind;
|
|
1146
|
+
throw new Error(`replayJournal: unsupported kind ${String(_exhaustive)}`);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
//#endregion
|
|
2937
1151
|
//#region src/editor/types.ts
|
|
2938
1152
|
/** Runtime list of the frozen, implemented kinds (for guards / introspection). */
|
|
2939
1153
|
const IMPLEMENTED_SEMANTIC_OP_KINDS = [
|
|
@@ -3286,6 +1500,24 @@ var MengineDocSession = class {
|
|
|
3286
1500
|
if (this.editorValue == null) throw new Error("mengine doc session is not started");
|
|
3287
1501
|
return this.editorValue;
|
|
3288
1502
|
}
|
|
1503
|
+
/**
|
|
1504
|
+
* Opaque version token of the local oplog (base64 `VersionVector.encode`).
|
|
1505
|
+
* Equality-comparable only: equal means no observed change (local or
|
|
1506
|
+
* remote-arrived) since the token was taken. Throws when not started.
|
|
1507
|
+
*/
|
|
1508
|
+
version() {
|
|
1509
|
+
if (this.adapterValue == null) throw new Error("mengine doc session is not started");
|
|
1510
|
+
return bytesToBase64(this.adapterValue.doc.oplogVersion().encode());
|
|
1511
|
+
}
|
|
1512
|
+
/**
|
|
1513
|
+
* The live document adapter, exposed for journal replay (commit channel).
|
|
1514
|
+
* Replaying through it still goes SemanticEditor → Loro → mengine-server —
|
|
1515
|
+
* no write bypass. Typed by the narrow interface on purpose.
|
|
1516
|
+
*/
|
|
1517
|
+
get documentAdapter() {
|
|
1518
|
+
if (this.adapterValue == null) throw new Error("mengine doc session is not started");
|
|
1519
|
+
return this.adapterValue;
|
|
1520
|
+
}
|
|
3289
1521
|
/** Current document snapshot (read model). */
|
|
3290
1522
|
snapshot() {
|
|
3291
1523
|
if (this.adapterValue == null) throw new Error("mengine doc session is not started");
|
|
@@ -3372,4 +1604,4 @@ var MengineDocSession = class {
|
|
|
3372
1604
|
}
|
|
3373
1605
|
};
|
|
3374
1606
|
//#endregion
|
|
3375
|
-
export { IMPLEMENTED_SEMANTIC_OP_KINDS, MedeoHttpDocStorage, MemoryDocStorage, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MirrorVideoDocumentAdapter, SchemaValidator, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, VIDEO_DOCUMENT_SCHEMA_VERSION, ValidationError, VideoDocumentValidationError, arrangeMainTrackSeamlessly, assertValidVideoDocument, base64ToBytes, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, derivePositionFromAbs, ensureLaneTrack, fillMainTrackTimeGaps, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, mainTrackRanges, partDurationMs, partUnionSchema, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, resolveAllSpeechOverlaps, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, snapshotToPlain, solveVideoDocument, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema };
|
|
1607
|
+
export { IMPLEMENTED_SEMANTIC_OP_KINDS, MedeoHttpDocStorage, MemoryDocStorage, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MirrorVideoDocumentAdapter, PlainMemoryAdapter, SchemaValidator, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, VIDEO_DOCUMENT_SCHEMA_VERSION, ValidationError, VideoDocumentValidationError, arrangeMainTrackSeamlessly, assertValidVideoDocument, base64ToBytes, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createEditSandbox, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, createPlainMemoryAdapter, derivePositionFromAbs, effectiveVideoClipDurationMs, ensureLaneTrack, fillMainTrackTimeGaps, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, mainTrackRanges, partDurationMs, partUnionSchema, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, replayJournal, resolveAllSpeechOverlaps, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, snapshotToPlain, solveVideoDocument, speedOf, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema, writeVideoDocumentToDraft };
|