@mengine/medeo-client 0.1.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/README.md +5 -0
- package/dist/chunk-D7D4PA-g.js +13 -0
- package/dist/index.d.ts +2373 -0
- package/dist/index.js +3374 -0
- package/package.json +51 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3374 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-D7D4PA-g.js";
|
|
2
|
+
import { Mirror, schema } from "loro-mirror";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { LoroDoc } from "loro-crdt";
|
|
5
|
+
import { ClientServerSynchronizer, DocManager } from "@mengine/sync";
|
|
6
|
+
import { DisposableSet, EventBus, Task } from "@mengine/utils";
|
|
7
|
+
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
|
+
//#region src/client/http-client.ts
|
|
22
|
+
var MengineHttpRequestError = class extends Error {
|
|
23
|
+
status;
|
|
24
|
+
payload;
|
|
25
|
+
constructor(status, payload) {
|
|
26
|
+
super(`mengine request failed: ${status}`);
|
|
27
|
+
this.status = status;
|
|
28
|
+
this.payload = payload;
|
|
29
|
+
this.name = "MengineHttpRequestError";
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var MengineHttpClient = class {
|
|
33
|
+
options;
|
|
34
|
+
fetchImpl;
|
|
35
|
+
constructor(options) {
|
|
36
|
+
this.options = options;
|
|
37
|
+
this.fetchImpl = options.fetchImpl ?? globalThis.fetch.bind(globalThis);
|
|
38
|
+
}
|
|
39
|
+
async fetchSnapshot() {
|
|
40
|
+
return await this.requestJson("snapshot");
|
|
41
|
+
}
|
|
42
|
+
async bootstrapSnapshot(snapshot) {
|
|
43
|
+
return await this.requestJson("bootstrap", {
|
|
44
|
+
method: "POST",
|
|
45
|
+
body: JSON.stringify({ snapshot: bytesToBase64(snapshot) })
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Loro VV-diff pull: send the caller's oplog `VersionVector.encode` as `from`
|
|
50
|
+
* (omit for a full pull) and receive exactly the updates it is missing plus
|
|
51
|
+
* the server's current VV. Replaces the old integer `after_update_id` cursor.
|
|
52
|
+
*/
|
|
53
|
+
async sync(fromVV) {
|
|
54
|
+
const query = fromVV != null ? `?from=${encodeURIComponent(bytesToBase64(fromVV))}` : "";
|
|
55
|
+
return await this.requestJson(`sync${query}`);
|
|
56
|
+
}
|
|
57
|
+
/** Audit trail: extracted metadata per accepted update, in log order. */
|
|
58
|
+
async audit() {
|
|
59
|
+
return await this.requestJson("audit");
|
|
60
|
+
}
|
|
61
|
+
async pushUpdate(update, baseVersion = null) {
|
|
62
|
+
const response = await this.requestJson("updates", {
|
|
63
|
+
method: "POST",
|
|
64
|
+
body: JSON.stringify({
|
|
65
|
+
update: bytesToBase64(update),
|
|
66
|
+
base_version: baseVersion
|
|
67
|
+
})
|
|
68
|
+
});
|
|
69
|
+
if (response.kind === "rejected") throw new MengineHttpRequestError(409, response);
|
|
70
|
+
return response;
|
|
71
|
+
}
|
|
72
|
+
eventsUrl() {
|
|
73
|
+
return this.endpoint("events");
|
|
74
|
+
}
|
|
75
|
+
headers() {
|
|
76
|
+
const headers = new Headers();
|
|
77
|
+
headers.set("accept", "application/json");
|
|
78
|
+
headers.set("content-type", "application/json");
|
|
79
|
+
if (this.options.authToken != null && this.options.authToken !== "") headers.set("authorization", `Bearer ${this.options.authToken}`);
|
|
80
|
+
const userId = typeof this.options.userId === "function" ? this.options.userId() : this.options.userId;
|
|
81
|
+
if (userId != null && userId !== "") headers.set("medeo-user-id", userId);
|
|
82
|
+
return headers;
|
|
83
|
+
}
|
|
84
|
+
async fetch(input, init) {
|
|
85
|
+
return await this.fetchImpl(input, init);
|
|
86
|
+
}
|
|
87
|
+
async requestJson(path, init = {}) {
|
|
88
|
+
const headers = this.headers();
|
|
89
|
+
new Headers(init.headers).forEach((value, key) => {
|
|
90
|
+
headers.set(key, value);
|
|
91
|
+
});
|
|
92
|
+
const response = await this.fetchImpl(this.endpoint(path), {
|
|
93
|
+
...init,
|
|
94
|
+
headers
|
|
95
|
+
});
|
|
96
|
+
const payload = await safeReadJson(response);
|
|
97
|
+
if (!response.ok) throw new MengineHttpRequestError(response.status, payload);
|
|
98
|
+
return payload;
|
|
99
|
+
}
|
|
100
|
+
endpoint(path) {
|
|
101
|
+
return `${this.options.httpOrigin.replace(/\/$/, "")}/api/mengine/docs/${encodeURIComponent(this.options.docId)}/${path}`;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
async function safeReadJson(response) {
|
|
105
|
+
const text = await response.text();
|
|
106
|
+
if (text.length === 0) return null;
|
|
107
|
+
try {
|
|
108
|
+
return JSON.parse(text);
|
|
109
|
+
} catch {
|
|
110
|
+
return text;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region src/client/sse.ts
|
|
115
|
+
async function readMengineEventStream(options) {
|
|
116
|
+
const onUpdate = (event) => {
|
|
117
|
+
options.onUpdate(event);
|
|
118
|
+
};
|
|
119
|
+
const response = await options.client.fetch(options.client.eventsUrl(), {
|
|
120
|
+
headers: options.client.headers(),
|
|
121
|
+
signal: options.signal
|
|
122
|
+
});
|
|
123
|
+
if (!response.ok) throw new Error(`mengine event stream failed: ${response.status}`);
|
|
124
|
+
const reader = response.body?.getReader();
|
|
125
|
+
if (reader == null) throw new Error("mengine event stream response has no readable body");
|
|
126
|
+
options.onOpen?.();
|
|
127
|
+
const decoder = new TextDecoder();
|
|
128
|
+
let buffer = "";
|
|
129
|
+
while (options.signal?.aborted !== true) {
|
|
130
|
+
const { done, value } = await reader.read();
|
|
131
|
+
if (done) return;
|
|
132
|
+
buffer += decoder.decode(value, { stream: true });
|
|
133
|
+
let splitAt = findSseFrameBoundary(buffer);
|
|
134
|
+
while (splitAt >= 0) {
|
|
135
|
+
const frame = buffer.slice(0, splitAt);
|
|
136
|
+
buffer = buffer.slice(buffer[splitAt] === "\r" ? splitAt + 4 : splitAt + 2);
|
|
137
|
+
handleSseFrame(frame, onUpdate);
|
|
138
|
+
splitAt = findSseFrameBoundary(buffer);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function handleSseFrame(frame, onUpdate) {
|
|
143
|
+
const event = parseSseFrame(frame);
|
|
144
|
+
if (event.event !== "update" || event.data.length === 0) return;
|
|
145
|
+
onUpdate(JSON.parse(event.data.join("\n")));
|
|
146
|
+
}
|
|
147
|
+
function parseSseFrame(frame) {
|
|
148
|
+
let event = null;
|
|
149
|
+
const data = [];
|
|
150
|
+
for (const line of frame.split(/\r?\n/)) if (line.startsWith("event:")) event = line.slice(6).trim();
|
|
151
|
+
else if (line.startsWith("data:")) data.push(line.slice(5).trimStart());
|
|
152
|
+
return {
|
|
153
|
+
event,
|
|
154
|
+
data
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function findSseFrameBoundary(buffer) {
|
|
158
|
+
const lf = buffer.indexOf("\n\n");
|
|
159
|
+
const crlf = buffer.indexOf("\r\n\r\n");
|
|
160
|
+
if (lf < 0) return crlf;
|
|
161
|
+
if (crlf < 0) return lf;
|
|
162
|
+
return Math.min(lf, crlf);
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/document/mirror-schema.ts
|
|
166
|
+
/**
|
|
167
|
+
* Declarative `loro-mirror` schema for `VideoDocument` — the canonical structural
|
|
168
|
+
* SSOT and live storage wire (replaces the hand-rolled `@mengine/schema`
|
|
169
|
+
* definition + adapter, see ADR 0008).
|
|
170
|
+
*
|
|
171
|
+
* Mirror gives an in-memory immutable state synced to the Loro doc by declarative
|
|
172
|
+
* diff, so the storage layer no longer hand-writes per-region reconcile or a
|
|
173
|
+
* transaction state machine. The shapes here store only authoritative facts
|
|
174
|
+
* (RFC 02 §6): per-item absolute time, `part_aggregations`, and total duration
|
|
175
|
+
* are projection-derived (cascade-solved at read time), so they are absent here.
|
|
176
|
+
*
|
|
177
|
+
* - tracks live in a single `tracks` `LoroMovableList` keyed by track id
|
|
178
|
+
* (reference/17 §4). Lane is expressed by each track's `parts_kind`, not by
|
|
179
|
+
* which container it lives in; order is the movable-list order itself. There is
|
|
180
|
+
* no `lane` / `lane_order` field, so two peers inserting tracks never collide
|
|
181
|
+
* on an order integer — list moves are conflict-free (RFC 03 §4). The legacy
|
|
182
|
+
* keyed-map (`LoroMapRecord` + an integer `lane_order`) and the prior three
|
|
183
|
+
* named containers are gone; the `main` / `above` / `below` three-pane view is
|
|
184
|
+
* rebuilt by the projection from `parts_kind`.
|
|
185
|
+
* - each track's `items` is a `LoroMovableList` keyed by `part_id`: a part is
|
|
186
|
+
* placed at most once per lane, so `part_id` is the stable placement identity.
|
|
187
|
+
* Reorder diffs to a real Loro `move`, preserving per-item CRDT identity on
|
|
188
|
+
* every lane (main and secondary alike).
|
|
189
|
+
* - `time_position` is the authoritative positioning fact (RFC 02 §4,
|
|
190
|
+
* reference/17 §3), carried as an opaque JSON blob string (a whole tagged-union
|
|
191
|
+
* value, last-writer-wins). The derived `fallback_abs_ms` snapshot sits beside
|
|
192
|
+
* it as its own number field — a separate LWW unit so a projection refresh
|
|
193
|
+
* never clobbers a `time_position` edit.
|
|
194
|
+
* - meta lives in a `meta` LoroMap: the mirror root only accepts container
|
|
195
|
+
* schemas, so meta scalars can't sit at the root as bare values. The domain
|
|
196
|
+
* `VideoDocument` keeps the same `meta` wrapper, so the shapes are isomorphic
|
|
197
|
+
* and the read/seed mapping is a near-identity.
|
|
198
|
+
* - `part_library` values are nested `LoroMap`s: the value is a `LoroMap` with
|
|
199
|
+
* four mutually-exclusive optional part sub-maps (`video_clip` / `speech` /
|
|
200
|
+
* `caption` / `bgm`), each a `LoroMap` whose fields are stored as real Loro
|
|
201
|
+
* sub-keys. This makes each field its own CRDT unit, so two peers editing
|
|
202
|
+
* different fields of the same part (e.g. one volume, one play_out) merge
|
|
203
|
+
* field-by-field instead of one whole-value overwrite. Field `required` mirrors
|
|
204
|
+
* the Smithy `@required` contract (see `video_draft_comp.smithy`). `kind` is not
|
|
205
|
+
* stored (the present sub-key names the kind); `duration_ms` is not stored (it
|
|
206
|
+
* is derived, reference/17 §5). Nested values that the engine does not edit
|
|
207
|
+
* field-by-field — `speed_shift` (a tagged union), `voice`, `caption_ids` — stay
|
|
208
|
+
* as opaque JSON-blob sub-keys via `transform`. The four sub-keys are optional,
|
|
209
|
+
* so the "exactly one part kind" union invariant is not enforced by the schema
|
|
210
|
+
* type; it is rebuilt by `draftToPartUnion` on read and gated by zod on write.
|
|
211
|
+
* - a caption's `style` is the one lazily-created optional child container, so
|
|
212
|
+
* `captionPart` sets `mergeableMapChildContainers: true` — two peers can
|
|
213
|
+
* concurrently first-create the same caption's `style` (one attribute each), and
|
|
214
|
+
* only a mergeable child converges instead of last-writer-wins dropping one. All
|
|
215
|
+
* other nested maps are keyed by a unique id (`part_library`) or created
|
|
216
|
+
* atomically with their parent (`partValue`'s part sub-map), so they never hit
|
|
217
|
+
* that concurrent-first-create fork and stay on plain `setContainer`. See the
|
|
218
|
+
* note on `captionPart` below.
|
|
219
|
+
* - `video_creation_settings` is an opaque JSON blob carried in a string via
|
|
220
|
+
* `transform`: a whole value, last-writer-wins (no field-level concurrent edits).
|
|
221
|
+
*/
|
|
222
|
+
/**
|
|
223
|
+
* JSON-blob transform for an opaque, last-writer-wins value carried in a Loro
|
|
224
|
+
* string. The field must be declared `required: false`: an absent field decodes
|
|
225
|
+
* to `undefined` (mirror never calls `decode`/`encode` for null/undefined),
|
|
226
|
+
* which is how we represent "no value" instead of encoding a `null` sentinel.
|
|
227
|
+
*/
|
|
228
|
+
function jsonTransform() {
|
|
229
|
+
return {
|
|
230
|
+
decode: (value) => JSON.parse(value),
|
|
231
|
+
encode: (value) => JSON.stringify(value),
|
|
232
|
+
isEqual: "encoded-value-equality"
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
const trackItem = schema.LoroMap({
|
|
236
|
+
part_id: schema.String(),
|
|
237
|
+
time_position: schema.String().transform(jsonTransform()),
|
|
238
|
+
fallback_abs_ms: schema.Number({ required: false })
|
|
239
|
+
});
|
|
240
|
+
const track = schema.LoroMap({
|
|
241
|
+
id: schema.String(),
|
|
242
|
+
parts_kind: schema.String({ required: false }),
|
|
243
|
+
is_hidden: schema.Boolean({ required: false }),
|
|
244
|
+
items: schema.LoroMovableList(trackItem, (item) => item.part_id)
|
|
245
|
+
});
|
|
246
|
+
const videoClipPart = schema.LoroMap({
|
|
247
|
+
id: schema.String(),
|
|
248
|
+
play_in: schema.Number(),
|
|
249
|
+
play_out: schema.Number(),
|
|
250
|
+
volume: schema.Number(),
|
|
251
|
+
origin_media_id: schema.String(),
|
|
252
|
+
speed_shift: schema.String({ required: false }).transform(jsonTransform())
|
|
253
|
+
}, { required: false });
|
|
254
|
+
const speechPart = schema.LoroMap({
|
|
255
|
+
id: schema.String(),
|
|
256
|
+
media_duration_ms: schema.Number(),
|
|
257
|
+
audio_script: schema.String(),
|
|
258
|
+
volume: schema.Number(),
|
|
259
|
+
audio_storage_key: schema.String(),
|
|
260
|
+
origin_speech_id: schema.String(),
|
|
261
|
+
voice: schema.String().transform(jsonTransform()),
|
|
262
|
+
caption_ids: schema.String().transform(jsonTransform())
|
|
263
|
+
}, { required: false });
|
|
264
|
+
const captionStyle = schema.LoroMap({
|
|
265
|
+
font_id: schema.String({ required: false }),
|
|
266
|
+
font_size: schema.Number({ required: false }),
|
|
267
|
+
font_color: schema.String({ required: false }),
|
|
268
|
+
font_weight: schema.Number({ required: false }),
|
|
269
|
+
entrance_animation: schema.String({ required: false }),
|
|
270
|
+
entrance_animation_duration_ms: schema.Number({ required: false }),
|
|
271
|
+
stroke_color: schema.String({ required: false }),
|
|
272
|
+
stroke_width: schema.Number({ required: false }),
|
|
273
|
+
position_x: schema.Number({ required: false }),
|
|
274
|
+
position_y: schema.Number({ required: false })
|
|
275
|
+
}, { required: false });
|
|
276
|
+
const captionPart = schema.LoroMap({
|
|
277
|
+
id: schema.String(),
|
|
278
|
+
initial_duration_ms: schema.Number(),
|
|
279
|
+
speech_part_id: schema.String(),
|
|
280
|
+
text: schema.String(),
|
|
281
|
+
start_ms: schema.Number(),
|
|
282
|
+
style: captionStyle
|
|
283
|
+
}, {
|
|
284
|
+
required: false,
|
|
285
|
+
mergeableMapChildContainers: true
|
|
286
|
+
});
|
|
287
|
+
const bgmPart = schema.LoroMap({
|
|
288
|
+
id: schema.String(),
|
|
289
|
+
audio_storage_key: schema.String(),
|
|
290
|
+
volume: schema.Number(),
|
|
291
|
+
origin_media_id: schema.String()
|
|
292
|
+
}, { required: false });
|
|
293
|
+
const partValue = schema.LoroMap({
|
|
294
|
+
video_clip: videoClipPart,
|
|
295
|
+
speech: speechPart,
|
|
296
|
+
caption: captionPart,
|
|
297
|
+
bgm: bgmPart
|
|
298
|
+
});
|
|
299
|
+
const videoDocumentMirrorSchema = schema({
|
|
300
|
+
meta: schema.LoroMap({
|
|
301
|
+
schema_version: schema.String({ required: false }),
|
|
302
|
+
draft_id: schema.String({ required: false }),
|
|
303
|
+
project_id: schema.String({ required: false }),
|
|
304
|
+
owner_id: schema.String({ required: false }),
|
|
305
|
+
thumbnail_storage_key: schema.String({ required: false }),
|
|
306
|
+
chat_session_id: schema.String({ required: false }),
|
|
307
|
+
video_creation_settings: schema.String({ required: false }).transform(jsonTransform()),
|
|
308
|
+
version: schema.Number({ required: false })
|
|
309
|
+
}),
|
|
310
|
+
timeline: schema.LoroMap({ unit_time_ms: schema.Number({ required: false }) }),
|
|
311
|
+
tracks: schema.LoroMovableList(track, (t) => t.id),
|
|
312
|
+
part_library: schema.LoroMapRecord(partValue)
|
|
313
|
+
});
|
|
314
|
+
/** Project a discriminated `PartUnion` into the draft's four-optional shape. */
|
|
315
|
+
function partUnionToDraft(part) {
|
|
316
|
+
if (part.video_clip != null) return { video_clip: omitKind(part.video_clip) };
|
|
317
|
+
if (part.speech != null) return { speech: omitKind(part.speech) };
|
|
318
|
+
if (part.caption != null) return { caption: omitKind(part.caption) };
|
|
319
|
+
return { bgm: omitKind(part.bgm) };
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Rebuild a discriminated `PartUnion` from a draft part value: pick the one
|
|
323
|
+
* present sub-key, restore its `kind`, and drop the `$cid`. Returns `undefined`
|
|
324
|
+
* when no part sub-key is present (an empty/placeholder value).
|
|
325
|
+
*/
|
|
326
|
+
function draftToPartUnion(value) {
|
|
327
|
+
if (value == null) return void 0;
|
|
328
|
+
const v = value;
|
|
329
|
+
if (v.video_clip != null) return { video_clip: withKind(v.video_clip, "video_clip") };
|
|
330
|
+
if (v.speech != null) return { speech: withKind(v.speech, "speech") };
|
|
331
|
+
if (v.caption != null) return { caption: withKind(v.caption, "caption") };
|
|
332
|
+
if (v.bgm != null) return { bgm: withKind(v.bgm, "bgm") };
|
|
333
|
+
}
|
|
334
|
+
/** Drop `kind` (not stored) and `$cid` from a part payload for the draft. */
|
|
335
|
+
function omitKind(part) {
|
|
336
|
+
const { kind: _kind, $cid: _cid, ...rest } = part;
|
|
337
|
+
return rest;
|
|
338
|
+
}
|
|
339
|
+
/** Restore `kind` (and drop `$cid`) on a part sub-map read from the draft. */
|
|
340
|
+
function withKind(part, kind) {
|
|
341
|
+
const { $cid: _cid, ...rest } = part;
|
|
342
|
+
return {
|
|
343
|
+
...rest,
|
|
344
|
+
kind
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/** Entries of a draft record (e.g. `tracks`) with `$cid` stripped. */
|
|
348
|
+
function recordEntries(record) {
|
|
349
|
+
if (record == null) return [];
|
|
350
|
+
const out = [];
|
|
351
|
+
for (const [key, value] of Object.entries(record)) {
|
|
352
|
+
if (key === "$cid") continue;
|
|
353
|
+
out.push([key, value]);
|
|
354
|
+
}
|
|
355
|
+
return out;
|
|
356
|
+
}
|
|
357
|
+
//#endregion
|
|
358
|
+
//#region src/document/types.ts
|
|
359
|
+
const VIDEO_DOCUMENT_SCHEMA_VERSION = "video-document/v0";
|
|
360
|
+
/** The linear speed multiplier of a `speed_shift`, defaulting to 1 (original). */
|
|
361
|
+
function speedOf(speedShift) {
|
|
362
|
+
const speed = speedShift?.config?.linear?.speed;
|
|
363
|
+
return typeof speed === "number" && Number.isFinite(speed) && speed > 0 ? speed : 1;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* A video clip's effective timeline duration, derived from authoritative facts
|
|
367
|
+
* (RFC 02, `reference/16` §4, reference/17 §5): the trim window
|
|
368
|
+
* `play_out - play_in` divided by the speed multiplier, rounded to integer ms.
|
|
369
|
+
* `play_in` / `play_out` are optional in the IDL but every write path sets them
|
|
370
|
+
* (defaulting to the whole media), and the legacy-ingest projection backfills the
|
|
371
|
+
* window from a legacy `duration_ms` — so an authoritative clip always carries a
|
|
372
|
+
* trim window and there is no stored `duration_ms` to fall back to. A clip with
|
|
373
|
+
* neither bound yields 0. Speeds the clip up (>1× → shorter) or down (<1×).
|
|
374
|
+
*/
|
|
375
|
+
function effectiveVideoClipDurationMs(clip) {
|
|
376
|
+
const speed = speedOf(clip.speed_shift);
|
|
377
|
+
const sourceMs = (clip.play_out ?? 0) - (clip.play_in ?? 0);
|
|
378
|
+
const effective = Math.round(sourceMs / speed);
|
|
379
|
+
return Number.isFinite(effective) && effective > 0 ? effective : 0;
|
|
380
|
+
}
|
|
381
|
+
//#endregion
|
|
382
|
+
//#region src/timeline-core/types.ts
|
|
383
|
+
/** Total document duration when there is no real content (matches FE bgm fallback). */
|
|
384
|
+
const TIMELINE_SKELETON_DURATION_MS = 2e4;
|
|
385
|
+
/** Empty placeholder clip marker: a video_clip part with no backing media. */
|
|
386
|
+
function isEmptyVideoClip(part) {
|
|
387
|
+
const clip = part?.video_clip;
|
|
388
|
+
if (clip == null) return false;
|
|
389
|
+
return clip.origin_media_id == null || clip.origin_media_id === "";
|
|
390
|
+
}
|
|
391
|
+
/** Clamp a duration to a non-negative integer (NaN/Infinity/negative → 0). */
|
|
392
|
+
function safeDurationMs(value) {
|
|
393
|
+
const n = Number(value);
|
|
394
|
+
if (!Number.isFinite(n) || n <= 0) return 0;
|
|
395
|
+
return Math.round(n);
|
|
396
|
+
}
|
|
397
|
+
function partDurationMs(doc, partId) {
|
|
398
|
+
const part = doc.part_library[partId];
|
|
399
|
+
if (part?.video_clip != null) return safeDurationMs(effectiveVideoClipDurationMs(part.video_clip));
|
|
400
|
+
if (part?.bgm != null) return doc.timeline.duration_ms > 0 ? doc.timeline.duration_ms : TIMELINE_SKELETON_DURATION_MS;
|
|
401
|
+
if (part?.speech != null) return safeDurationMs(part.speech.media_duration_ms);
|
|
402
|
+
if (part?.caption != null) return safeDurationMs(part.caption.initial_duration_ms);
|
|
403
|
+
return 0;
|
|
404
|
+
}
|
|
405
|
+
//#endregion
|
|
406
|
+
//#region src/timeline-core/cascade.ts
|
|
407
|
+
/**
|
|
408
|
+
* Canonical timeline cascade primitives (ADR 0009).
|
|
409
|
+
*
|
|
410
|
+
* Single source of truth for "how an edit's connected regions move": main-track
|
|
411
|
+
* seamless layout, aggregation position sync + reassignment, total-duration
|
|
412
|
+
* recompute, speech-overlap resolution, gap filling. Reconciled per ADR 0009 §4:
|
|
413
|
+
*
|
|
414
|
+
* - product behavior follows the FE current implementation;
|
|
415
|
+
* - all times are integer ms — positions/durations are rounded, never floated;
|
|
416
|
+
* - function decomposition follows agent-harness (the Python-derived structure).
|
|
417
|
+
*
|
|
418
|
+
* Every function mutates the `TimelineDoc` in place (the editor runs them inside
|
|
419
|
+
* one immer `transact`, so in-place edits diff correctly).
|
|
420
|
+
*/
|
|
421
|
+
/**
|
|
422
|
+
* Lay the main track out head-to-tail from 0, rewriting each item's
|
|
423
|
+
* `abs_time_position`. Items whose part is missing from the library are dropped.
|
|
424
|
+
*/
|
|
425
|
+
function arrangeMainTrackSeamlessly(doc) {
|
|
426
|
+
const kept = [];
|
|
427
|
+
let cursor = 0;
|
|
428
|
+
for (const item of doc.main_track) {
|
|
429
|
+
if (doc.part_library[item.part_id] == null) continue;
|
|
430
|
+
item.abs_time_position = cursor;
|
|
431
|
+
cursor += partDurationMs(doc, item.part_id);
|
|
432
|
+
kept.push(item);
|
|
433
|
+
}
|
|
434
|
+
doc.main_track = kept;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Sync attached parts' absolute positions from their host:
|
|
438
|
+
* speech.abs = host_video.abs + relative_time_position
|
|
439
|
+
* caption.abs = speech.abs + caption.start_ms
|
|
440
|
+
*/
|
|
441
|
+
function syncAggregatedClipsTimePosition(doc) {
|
|
442
|
+
const videoByPart = indexByPart(doc.main_track);
|
|
443
|
+
const speechByPart = indexByPart(doc.speech_track);
|
|
444
|
+
for (const aggregation of doc.aggregations) {
|
|
445
|
+
const videoItem = videoByPart.get(aggregation.body_part_id);
|
|
446
|
+
if (videoItem == null) continue;
|
|
447
|
+
for (const attachment of aggregation.attachments) {
|
|
448
|
+
const speechItem = speechByPart.get(attachment.part_id);
|
|
449
|
+
if (speechItem == null) continue;
|
|
450
|
+
speechItem.abs_time_position = videoItem.abs_time_position + attachment.relative_time_position;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
for (const captionItem of doc.caption_track) {
|
|
454
|
+
const captionPart = doc.part_library[captionItem.part_id]?.caption;
|
|
455
|
+
if (captionPart == null) continue;
|
|
456
|
+
const speechItem = captionPart.speech_part_id == null ? void 0 : speechByPart.get(captionPart.speech_part_id);
|
|
457
|
+
if (speechItem == null) continue;
|
|
458
|
+
captionItem.abs_time_position = speechItem.abs_time_position + safeDurationMs(captionPart.start_ms);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Reassign each speech to the video clip whose time range contains its start,
|
|
463
|
+
* rebuilding `aggregations`. A speech before the first clip or after the last
|
|
464
|
+
* falls back to the first / last clip respectively (FE: see §4 note — FE falls
|
|
465
|
+
* back to last only; we keep the harness two-sided fallback because a speech
|
|
466
|
+
* dragged before clip 0 belonging to the last clip is clearly wrong, and the FE
|
|
467
|
+
* single-sided rule is an acknowledged rough edge). `relative_time_position` is
|
|
468
|
+
* clamped to a non-negative integer.
|
|
469
|
+
*/
|
|
470
|
+
function reassignSpeechesToVideoClipsByTime(doc) {
|
|
471
|
+
const ranges = doc.main_track.filter((item) => doc.part_library[item.part_id] != null).map((item) => ({
|
|
472
|
+
part_id: item.part_id,
|
|
473
|
+
start: item.abs_time_position,
|
|
474
|
+
end: item.abs_time_position + partDurationMs(doc, item.part_id)
|
|
475
|
+
}));
|
|
476
|
+
if (ranges.length === 0) {
|
|
477
|
+
doc.aggregations = [];
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const rebuilt = /* @__PURE__ */ new Map();
|
|
481
|
+
for (const speechItem of doc.speech_track) {
|
|
482
|
+
const start = speechItem.abs_time_position;
|
|
483
|
+
const targetRange = ranges.find((r) => r.start <= start && start < r.end) ?? (start < ranges[0].start ? ranges[0] : ranges[ranges.length - 1]);
|
|
484
|
+
const relative = Math.max(0, Math.round(start - targetRange.start));
|
|
485
|
+
const targetId = targetRange.part_id;
|
|
486
|
+
let aggregation = rebuilt.get(targetId);
|
|
487
|
+
if (aggregation == null) {
|
|
488
|
+
aggregation = {
|
|
489
|
+
body_part_id: targetId,
|
|
490
|
+
attachments: []
|
|
491
|
+
};
|
|
492
|
+
rebuilt.set(targetId, aggregation);
|
|
493
|
+
}
|
|
494
|
+
aggregation.attachments.push({
|
|
495
|
+
part_id: speechItem.part_id,
|
|
496
|
+
relative_time_position: relative
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
doc.aggregations = ranges.map((r) => rebuilt.get(r.part_id)).filter((a) => a != null);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Recompute `timeline.duration_ms` as the max end (abs + duration) across main,
|
|
503
|
+
* speech, and caption lanes (BGM does not extend the timeline).
|
|
504
|
+
*
|
|
505
|
+
* BGM has no authoritative duration (RFC 02 / `reference/16` §0b): its effective
|
|
506
|
+
* length is always the timeline total, so it is not written back here — the
|
|
507
|
+
* projection derives it from `timeline.duration_ms` on read (`partDurationMs`
|
|
508
|
+
* returns the timeline total for a bgm part). The empty-document 20s skeleton is
|
|
509
|
+
* applied at that read step, not stored.
|
|
510
|
+
*/
|
|
511
|
+
function recalculateTimelineDuration(doc) {
|
|
512
|
+
const max = Math.max(laneEndMs(doc, doc.main_track), laneEndMs(doc, doc.speech_track), laneEndMs(doc, doc.caption_track));
|
|
513
|
+
doc.timeline.duration_ms = max;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Resolve one speech overlap by shifting the overlapping speech's host video
|
|
517
|
+
* (and every clip after it) right. Returns true when one overlap was resolved;
|
|
518
|
+
* callers loop until it returns false. The compared range is the speech merged
|
|
519
|
+
* with its captions: start = min(speech.start, captions.start) (FE behavior),
|
|
520
|
+
* end = max(speech.end, captions.end).
|
|
521
|
+
*/
|
|
522
|
+
function resolveSpeechOverlapByShiftingVideos(doc) {
|
|
523
|
+
const speeches = doc.speech_track;
|
|
524
|
+
for (let i = 1; i < speeches.length; i++) {
|
|
525
|
+
const prev = speechWithCaptionsRange(doc, speeches[i - 1]);
|
|
526
|
+
const curr = speechWithCaptionsRange(doc, speeches[i]);
|
|
527
|
+
if (prev == null || curr == null) continue;
|
|
528
|
+
if (curr.start >= prev.end) continue;
|
|
529
|
+
const overlapMs = Math.round(prev.end - curr.start);
|
|
530
|
+
const hostId = hostVideoOf(doc, speeches[i].part_id);
|
|
531
|
+
if (hostId == null) continue;
|
|
532
|
+
const fromIndex = doc.main_track.findIndex((item) => item.part_id === hostId);
|
|
533
|
+
if (fromIndex < 0) continue;
|
|
534
|
+
for (let idx = fromIndex; idx < doc.main_track.length; idx++) doc.main_track[idx].abs_time_position += overlapMs;
|
|
535
|
+
syncAggregatedClipsTimePosition(doc);
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
return false;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Resolve speech overlaps for one cascade pass. Mirrors the authoritative FE
|
|
542
|
+
* `ensureNoOverlappingClips`, which is documented to resolve AT MOST ONE overlap
|
|
543
|
+
* per cascade and is invoked exactly once at every FE call site — the supported
|
|
544
|
+
* ops each produce at most one new overlap. It is NOT a fixpoint loop: shifting a
|
|
545
|
+
* host right also moves every speech anchored to it, so two speeches sharing a
|
|
546
|
+
* host can never be separated by shifting. Looping to a "fixed point" there does
|
|
547
|
+
* not converge — it accumulates the same overlap every iteration and pushes the
|
|
548
|
+
* clip arbitrarily far right (e.g. a sped-up clip landing at ~287k ms instead of
|
|
549
|
+
* its seamless slot). A single pass matches FE product behavior and terminates.
|
|
550
|
+
*/
|
|
551
|
+
function resolveAllSpeechOverlaps(doc) {
|
|
552
|
+
resolveSpeechOverlapByShiftingVideos(doc);
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Make the main track gapless by adjusting/merging empty placeholder clips or
|
|
556
|
+
* inserting new ones between real clips. Mirrors the harness four-case rule, but
|
|
557
|
+
* the merge of two adjacent empty clips keeps the earlier clip (harness Case 4).
|
|
558
|
+
* Requires `makeEmptyPart` to mint a placeholder part (the editor supplies an
|
|
559
|
+
* id generator).
|
|
560
|
+
*/
|
|
561
|
+
function fillMainTrackTimeGaps(doc, makeEmptyPart) {
|
|
562
|
+
const items = doc.main_track;
|
|
563
|
+
let i = 0;
|
|
564
|
+
while (i < items.length) {
|
|
565
|
+
const current = items[i];
|
|
566
|
+
const currentPart = doc.part_library[current.part_id];
|
|
567
|
+
if (currentPart == null) {
|
|
568
|
+
i += 1;
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
const currentEnd = current.abs_time_position + partDurationMs(doc, current.part_id);
|
|
572
|
+
if (i + 1 >= items.length) break;
|
|
573
|
+
const next = items[i + 1];
|
|
574
|
+
const nextPart = doc.part_library[next.part_id];
|
|
575
|
+
if (nextPart == null) {
|
|
576
|
+
i += 1;
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
const gap = next.abs_time_position - currentEnd;
|
|
580
|
+
if (gap > 0) {
|
|
581
|
+
if (isEmptyVideoClip(currentPart)) {
|
|
582
|
+
extendEmptyClip(currentPart.video_clip, gap);
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
if (isEmptyVideoClip(nextPart)) {
|
|
586
|
+
next.abs_time_position -= gap;
|
|
587
|
+
extendEmptyClip(nextPart.video_clip, gap);
|
|
588
|
+
i += 1;
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
const { partId } = makeEmptyPart(gap);
|
|
592
|
+
doc.part_library[partId] = { video_clip: {
|
|
593
|
+
id: partId,
|
|
594
|
+
kind: "video_clip",
|
|
595
|
+
play_in: 0,
|
|
596
|
+
play_out: gap,
|
|
597
|
+
volume: 0,
|
|
598
|
+
origin_media_id: ""
|
|
599
|
+
} };
|
|
600
|
+
items.splice(i + 1, 0, {
|
|
601
|
+
part_id: partId,
|
|
602
|
+
time_position: { mode: "sequential" },
|
|
603
|
+
abs_time_position: currentEnd
|
|
604
|
+
});
|
|
605
|
+
i += 1;
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
if (gap === 0 && isEmptyVideoClip(currentPart) && isEmptyVideoClip(nextPart)) {
|
|
609
|
+
extendEmptyClip(currentPart.video_clip, partDurationMs(doc, next.part_id));
|
|
610
|
+
items.splice(i + 1, 1);
|
|
611
|
+
delete doc.part_library[next.part_id];
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
i += 1;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
function indexByPart(items) {
|
|
618
|
+
const map = /* @__PURE__ */ new Map();
|
|
619
|
+
for (const item of items) map.set(item.part_id, item);
|
|
620
|
+
return map;
|
|
621
|
+
}
|
|
622
|
+
function laneEndMs(doc, items) {
|
|
623
|
+
let max = 0;
|
|
624
|
+
for (const item of items) {
|
|
625
|
+
if (doc.part_library[item.part_id] == null) continue;
|
|
626
|
+
const end = item.abs_time_position + partDurationMs(doc, item.part_id);
|
|
627
|
+
if (end > max) max = end;
|
|
628
|
+
}
|
|
629
|
+
return max;
|
|
630
|
+
}
|
|
631
|
+
function extendEmptyClip(clip, byMs) {
|
|
632
|
+
clip.play_out = safeDurationMs(clip.play_out) + byMs;
|
|
633
|
+
}
|
|
634
|
+
/** speech merged with its captions: start = min, end = max. */
|
|
635
|
+
function speechWithCaptionsRange(doc, speechItem) {
|
|
636
|
+
const speech = doc.part_library[speechItem.part_id]?.speech;
|
|
637
|
+
if (speech == null) return null;
|
|
638
|
+
let start = speechItem.abs_time_position;
|
|
639
|
+
let end = speechItem.abs_time_position + safeDurationMs(speech.media_duration_ms);
|
|
640
|
+
for (const captionId of captionIdsOf(speech)) {
|
|
641
|
+
const captionItem = doc.caption_track.find((item) => item.part_id === captionId);
|
|
642
|
+
const captionPart = doc.part_library[captionId]?.caption;
|
|
643
|
+
if (captionItem == null || captionPart == null) continue;
|
|
644
|
+
const cStart = captionItem.abs_time_position;
|
|
645
|
+
const cEnd = captionItem.abs_time_position + safeDurationMs(captionPart.initial_duration_ms);
|
|
646
|
+
if (cStart < start) start = cStart;
|
|
647
|
+
if (cEnd > end) end = cEnd;
|
|
648
|
+
}
|
|
649
|
+
return {
|
|
650
|
+
start,
|
|
651
|
+
end
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
function captionIdsOf(speech) {
|
|
655
|
+
return (speech.caption_ids ?? []).filter((id) => id != null);
|
|
656
|
+
}
|
|
657
|
+
function hostVideoOf(doc, speechPartId) {
|
|
658
|
+
for (const aggregation of doc.aggregations) if (aggregation.attachments.some((att) => att.part_id === speechPartId)) return aggregation.body_part_id;
|
|
659
|
+
return null;
|
|
660
|
+
}
|
|
661
|
+
//#endregion
|
|
662
|
+
//#region src/timeline-core/entrypoints.ts
|
|
663
|
+
/**
|
|
664
|
+
* The full solve pipeline: arrange → sync → reassign → resolve-overlap →
|
|
665
|
+
* fill-gaps → recalc. The single cascade the read-side projection runs; ops
|
|
666
|
+
* never call it (they write only facts — RFC 02 §7/§10).
|
|
667
|
+
*/
|
|
668
|
+
function cascadeAfterVideoClipChanges(doc, makeEmptyPart) {
|
|
669
|
+
arrangeMainTrackSeamlessly(doc);
|
|
670
|
+
syncAggregatedClipsTimePosition(doc);
|
|
671
|
+
reassignSpeechesToVideoClipsByTime(doc);
|
|
672
|
+
resolveAllSpeechOverlaps(doc);
|
|
673
|
+
fillMainTrackTimeGaps(doc, makeEmptyPart);
|
|
674
|
+
recalculateTimelineDuration(doc);
|
|
675
|
+
}
|
|
676
|
+
//#endregion
|
|
677
|
+
//#region src/timeline-core/bridge.ts
|
|
678
|
+
/**
|
|
679
|
+
* Solve a `VideoDocument` (authoritative, position-only) into its derived
|
|
680
|
+
* read-view: absolute time per item, `part_aggregations`, and total duration.
|
|
681
|
+
* This is the read side of the single-directional flow — never written back.
|
|
682
|
+
*/
|
|
683
|
+
function solveVideoDocument(document) {
|
|
684
|
+
const doc = videoDocumentToTimelineDoc(document);
|
|
685
|
+
let counter = 0;
|
|
686
|
+
cascadeAfterVideoClipChanges(doc, () => ({ partId: `empty_${counter++}` }));
|
|
687
|
+
const absByPartId = /* @__PURE__ */ new Map();
|
|
688
|
+
for (const item of doc.main_track) absByPartId.set(item.part_id, item.abs_time_position);
|
|
689
|
+
for (const item of doc.speech_track) absByPartId.set(item.part_id, item.abs_time_position);
|
|
690
|
+
for (const item of doc.caption_track) absByPartId.set(item.part_id, item.abs_time_position);
|
|
691
|
+
for (const item of doc.bgm_track) absByPartId.set(item.part_id, item.abs_time_position);
|
|
692
|
+
return {
|
|
693
|
+
absByPartId,
|
|
694
|
+
aggregations: doc.aggregations.map((aggregation) => ({
|
|
695
|
+
body_part_id: aggregation.body_part_id,
|
|
696
|
+
attachments: aggregation.attachments.map((a) => ({
|
|
697
|
+
part_id: a.part_id,
|
|
698
|
+
relative_time_position: a.relative_time_position
|
|
699
|
+
}))
|
|
700
|
+
})),
|
|
701
|
+
durationMs: doc.timeline.duration_ms,
|
|
702
|
+
partLibrary: doc.part_library
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
function videoDocumentToTimelineDoc(document) {
|
|
706
|
+
const partLibrary = {};
|
|
707
|
+
for (const [partId, part] of Object.entries(document.part_library ?? {})) partLibrary[partId] = part;
|
|
708
|
+
const laneItems = (track) => (track?.items ?? []).map((item) => ({
|
|
709
|
+
part_id: item.part_id,
|
|
710
|
+
time_position: item.time_position,
|
|
711
|
+
abs_time_position: seedAbs(item.time_position, item.fallback_abs_ms),
|
|
712
|
+
fallback_abs_ms: item.fallback_abs_ms
|
|
713
|
+
}));
|
|
714
|
+
const tracks = document.tracks ?? [];
|
|
715
|
+
let mainItems = [];
|
|
716
|
+
let speechItems = [];
|
|
717
|
+
let bgmItems = [];
|
|
718
|
+
let captionItems = [];
|
|
719
|
+
for (const t of tracks) switch (t.parts_kind) {
|
|
720
|
+
case "video_clip":
|
|
721
|
+
mainItems = mainItems.concat(laneItems(t));
|
|
722
|
+
break;
|
|
723
|
+
case "speech":
|
|
724
|
+
speechItems = speechItems.concat(laneItems(t));
|
|
725
|
+
break;
|
|
726
|
+
case "bgm":
|
|
727
|
+
bgmItems = bgmItems.concat(laneItems(t));
|
|
728
|
+
break;
|
|
729
|
+
case "caption":
|
|
730
|
+
captionItems = captionItems.concat(laneItems(t));
|
|
731
|
+
break;
|
|
732
|
+
}
|
|
733
|
+
return {
|
|
734
|
+
main_track: mainItems,
|
|
735
|
+
speech_track: speechItems,
|
|
736
|
+
caption_track: captionItems,
|
|
737
|
+
bgm_track: bgmItems,
|
|
738
|
+
part_library: partLibrary,
|
|
739
|
+
aggregations: seedAggregations(mainItems, speechItems),
|
|
740
|
+
timeline: {
|
|
741
|
+
duration_ms: 0,
|
|
742
|
+
unit_time_ms: document.timeline?.unit_time_ms ?? 0
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Seed an item's solve-variable `abs_time_position` from its `time_position` (and
|
|
748
|
+
* the `fallback_abs_ms` snapshot carried beside it): `absolute` → `offsetMs`;
|
|
749
|
+
* `anchored` → `fallbackAbsMs` (last solved position, also the orphan-recovery
|
|
750
|
+
* anchor when the host is gone); `sequential` → 0 (the cascade lays the main
|
|
751
|
+
* track out head-to-tail). The cascade then resolves anchored items against their
|
|
752
|
+
* host, so this seed only needs to put each item somewhere plausible for the
|
|
753
|
+
* first reassign/overlap pass.
|
|
754
|
+
*/
|
|
755
|
+
function seedAbs(position, fallbackAbsMs) {
|
|
756
|
+
switch (position.mode) {
|
|
757
|
+
case "absolute": return Math.round(position.offsetMs);
|
|
758
|
+
case "anchored": return Math.round(fallbackAbsMs ?? 0);
|
|
759
|
+
case "sequential": return 0;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Build the cascade `aggregations` parent-map from anchored items: an anchored
|
|
764
|
+
* item's `anchorPartId` is the host's `part_id` (= `body_part_id`), `offsetMs`
|
|
765
|
+
* is `relative_time_position`. Only main-track (video) hosts form aggregations;
|
|
766
|
+
* caption→speech relations are recomputed by the cascade from `caption.start_ms`.
|
|
767
|
+
*/
|
|
768
|
+
function seedAggregations(mainItems, speechItems) {
|
|
769
|
+
const mainPartIds = new Set(mainItems.map((item) => item.part_id));
|
|
770
|
+
const byHost = /* @__PURE__ */ new Map();
|
|
771
|
+
const order = [];
|
|
772
|
+
for (const speech of speechItems) {
|
|
773
|
+
if (speech.time_position.mode !== "anchored") continue;
|
|
774
|
+
const host = speech.time_position.anchorPartId;
|
|
775
|
+
if (!mainPartIds.has(host)) continue;
|
|
776
|
+
let aggregation = byHost.get(host);
|
|
777
|
+
if (aggregation == null) {
|
|
778
|
+
aggregation = {
|
|
779
|
+
body_part_id: host,
|
|
780
|
+
attachments: []
|
|
781
|
+
};
|
|
782
|
+
byHost.set(host, aggregation);
|
|
783
|
+
order.push(host);
|
|
784
|
+
}
|
|
785
|
+
aggregation.attachments.push({
|
|
786
|
+
part_id: speech.part_id,
|
|
787
|
+
relative_time_position: speech.time_position.offsetMs
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
return order.map((host) => byHost.get(host));
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Lane-stacking rank for the single `tracks` list (reference/17 §4): caption
|
|
794
|
+
* (above) sits before the video_clip main track, which sits before speech / bgm
|
|
795
|
+
* (below). The `tracks` array is kept in this top-to-bottom order so a freshly
|
|
796
|
+
* minted track lands in the right place and the projection's three-pane rebuild
|
|
797
|
+
* stays deterministic.
|
|
798
|
+
*/
|
|
799
|
+
function laneRank(kind) {
|
|
800
|
+
switch (kind) {
|
|
801
|
+
case "caption": return 0;
|
|
802
|
+
case "video_clip": return 1;
|
|
803
|
+
case "speech":
|
|
804
|
+
case "bgm": return 2;
|
|
805
|
+
default: return 3;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Insert a freshly-minted track into `tracks` at the position that keeps the
|
|
810
|
+
* lane-stacking order (caption → main → speech/bgm). Inserts before the first
|
|
811
|
+
* track whose rank is strictly greater, so same-rank tracks keep insertion order.
|
|
812
|
+
*/
|
|
813
|
+
function insertTrackByLaneOrder(tracks, track) {
|
|
814
|
+
const rank = laneRank(track.parts_kind);
|
|
815
|
+
const at = tracks.findIndex((t) => laneRank(t?.parts_kind) > rank);
|
|
816
|
+
if (at < 0) tracks.push(track);
|
|
817
|
+
else tracks.splice(at, 0, track);
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Locate a lane's track row in the single `tracks` list by kind, minting an empty
|
|
821
|
+
* row in lane-stacking order if absent (reference/17 §4: lane = `parts_kind`).
|
|
822
|
+
* Ops use this to write authoritative items onto the right lane. The track id
|
|
823
|
+
* mirrors the seed convention (`<kind>_track`).
|
|
824
|
+
*/
|
|
825
|
+
function ensureLaneTrack(draft, kind) {
|
|
826
|
+
draft.tracks ??= [];
|
|
827
|
+
let track = draft.tracks.find((t) => t?.parts_kind === kind);
|
|
828
|
+
if (track == null) {
|
|
829
|
+
track = {
|
|
830
|
+
id: kind === "video_clip" ? "main_track" : `${kind}_track`,
|
|
831
|
+
parts_kind: kind,
|
|
832
|
+
is_hidden: void 0,
|
|
833
|
+
items: []
|
|
834
|
+
};
|
|
835
|
+
insertTrackByLaneOrder(draft.tracks, track);
|
|
836
|
+
}
|
|
837
|
+
track.items ??= [];
|
|
838
|
+
return track;
|
|
839
|
+
}
|
|
840
|
+
/** Find a secondary lane's track row without minting it. */
|
|
841
|
+
function findLaneTrack(draft, kind) {
|
|
842
|
+
return (draft.tracks ?? []).find((t) => t?.parts_kind === kind);
|
|
843
|
+
}
|
|
844
|
+
//#endregion
|
|
845
|
+
//#region src/document/zod-schema.ts
|
|
846
|
+
const partKindSchema = z.enum([
|
|
847
|
+
"video_clip",
|
|
848
|
+
"speech",
|
|
849
|
+
"caption",
|
|
850
|
+
"bgm"
|
|
851
|
+
]);
|
|
852
|
+
const finiteNumber = z.number().finite();
|
|
853
|
+
const trackItemTimePositionSchema = z.discriminatedUnion("mode", [
|
|
854
|
+
z.object({ mode: z.literal("sequential") }).passthrough(),
|
|
855
|
+
z.object({
|
|
856
|
+
mode: z.literal("anchored"),
|
|
857
|
+
anchorPartId: z.string(),
|
|
858
|
+
offsetMs: finiteNumber
|
|
859
|
+
}).passthrough(),
|
|
860
|
+
z.object({
|
|
861
|
+
mode: z.literal("absolute"),
|
|
862
|
+
offsetMs: finiteNumber
|
|
863
|
+
}).passthrough()
|
|
864
|
+
]);
|
|
865
|
+
const trackItemSchema = z.object({
|
|
866
|
+
part_id: z.string(),
|
|
867
|
+
time_position: trackItemTimePositionSchema,
|
|
868
|
+
fallback_abs_ms: finiteNumber.optional()
|
|
869
|
+
}).passthrough();
|
|
870
|
+
const trackSchema = z.object({
|
|
871
|
+
id: z.string().optional(),
|
|
872
|
+
parts_kind: partKindSchema.optional(),
|
|
873
|
+
is_hidden: z.boolean().optional(),
|
|
874
|
+
items: z.array(trackItemSchema).optional()
|
|
875
|
+
}).passthrough();
|
|
876
|
+
const speedShiftSchema$1 = z.object({
|
|
877
|
+
category: z.string().optional(),
|
|
878
|
+
mode: z.string().optional(),
|
|
879
|
+
config: z.object({ linear: z.object({ speed: finiteNumber.optional() }).passthrough().optional() }).passthrough().optional()
|
|
880
|
+
}).passthrough();
|
|
881
|
+
const videoClipPartSchema = z.object({
|
|
882
|
+
id: z.string().optional(),
|
|
883
|
+
kind: z.literal("video_clip"),
|
|
884
|
+
duration_ms: finiteNumber.optional(),
|
|
885
|
+
play_in: finiteNumber,
|
|
886
|
+
play_out: finiteNumber,
|
|
887
|
+
volume: finiteNumber,
|
|
888
|
+
origin_media_id: z.string(),
|
|
889
|
+
speed_shift: speedShiftSchema$1.optional()
|
|
890
|
+
}).passthrough();
|
|
891
|
+
const speechPartSchema = z.object({
|
|
892
|
+
id: z.string().optional(),
|
|
893
|
+
kind: z.literal("speech"),
|
|
894
|
+
media_duration_ms: finiteNumber,
|
|
895
|
+
duration_ms: finiteNumber.optional(),
|
|
896
|
+
audio_script: z.string(),
|
|
897
|
+
volume: finiteNumber,
|
|
898
|
+
audio_storage_key: z.string(),
|
|
899
|
+
origin_speech_id: z.string(),
|
|
900
|
+
voice: z.unknown(),
|
|
901
|
+
caption_ids: z.array(z.string())
|
|
902
|
+
}).passthrough();
|
|
903
|
+
const captionPartSchema = z.object({
|
|
904
|
+
id: z.string().optional(),
|
|
905
|
+
kind: z.literal("caption"),
|
|
906
|
+
initial_duration_ms: finiteNumber,
|
|
907
|
+
speech_part_id: z.string(),
|
|
908
|
+
text: z.string(),
|
|
909
|
+
start_ms: finiteNumber,
|
|
910
|
+
style: z.object({
|
|
911
|
+
font_id: z.string().optional(),
|
|
912
|
+
font_size: finiteNumber.optional(),
|
|
913
|
+
font_color: z.string().optional(),
|
|
914
|
+
font_weight: finiteNumber.optional(),
|
|
915
|
+
entrance_animation: z.string().optional(),
|
|
916
|
+
entrance_animation_duration_ms: finiteNumber.optional(),
|
|
917
|
+
stroke_color: z.string().optional(),
|
|
918
|
+
stroke_width: finiteNumber.optional(),
|
|
919
|
+
position_x: finiteNumber.optional(),
|
|
920
|
+
position_y: finiteNumber.optional()
|
|
921
|
+
}).passthrough().optional()
|
|
922
|
+
}).passthrough();
|
|
923
|
+
const bgmPartSchema = z.object({
|
|
924
|
+
id: z.string().optional(),
|
|
925
|
+
kind: z.literal("bgm"),
|
|
926
|
+
audio_storage_key: z.string(),
|
|
927
|
+
volume: finiteNumber,
|
|
928
|
+
origin_media_id: z.string()
|
|
929
|
+
}).passthrough();
|
|
930
|
+
const partUnionSchema = z.union([
|
|
931
|
+
z.object({ video_clip: videoClipPartSchema }).passthrough(),
|
|
932
|
+
z.object({ speech: speechPartSchema }).passthrough(),
|
|
933
|
+
z.object({ caption: captionPartSchema }).passthrough(),
|
|
934
|
+
z.object({ bgm: bgmPartSchema }).passthrough()
|
|
935
|
+
]).refine((part) => {
|
|
936
|
+
const p = part;
|
|
937
|
+
return [
|
|
938
|
+
"video_clip",
|
|
939
|
+
"speech",
|
|
940
|
+
"caption",
|
|
941
|
+
"bgm"
|
|
942
|
+
].filter((k) => p[k] != null).length === 1;
|
|
943
|
+
}, { message: "A part_library value must have exactly one of video_clip / speech / caption / bgm" });
|
|
944
|
+
const videoDocumentMetaSchema = z.object({
|
|
945
|
+
schema_version: z.literal(VIDEO_DOCUMENT_SCHEMA_VERSION),
|
|
946
|
+
draft_id: z.string().optional(),
|
|
947
|
+
project_id: z.string().optional(),
|
|
948
|
+
owner_id: z.string().optional(),
|
|
949
|
+
thumbnail_storage_key: z.string().optional(),
|
|
950
|
+
chat_session_id: z.string().optional(),
|
|
951
|
+
video_creation_settings: z.unknown().optional(),
|
|
952
|
+
version: finiteNumber.optional()
|
|
953
|
+
}).passthrough();
|
|
954
|
+
const videoDocumentSchema = z.object({
|
|
955
|
+
meta: videoDocumentMetaSchema,
|
|
956
|
+
timeline: z.object({ unit_time_ms: finiteNumber.optional() }).passthrough().optional(),
|
|
957
|
+
tracks: z.array(trackSchema).optional(),
|
|
958
|
+
part_library: z.record(z.string(), partUnionSchema).optional()
|
|
959
|
+
}).passthrough().refine((doc) => (doc.tracks ?? []).filter((t) => t.parts_kind === "video_clip").length <= 1, {
|
|
960
|
+
message: "A VideoDocument may have at most one video_clip (main) track",
|
|
961
|
+
path: ["tracks"]
|
|
962
|
+
});
|
|
963
|
+
//#endregion
|
|
964
|
+
//#region src/document/validation.ts
|
|
965
|
+
/**
|
|
966
|
+
* Business-level schema guard for `VideoDocument` (RFC 03 §9). It is the gate
|
|
967
|
+
* that decides whether an arbitrary value is a *legal* `VideoDocument` before it
|
|
968
|
+
* is written into Loro — distinct from two neighbours:
|
|
969
|
+
*
|
|
970
|
+
* - `zod-schema.ts` (`videoDocumentSchema`) checks structure/shape only; this
|
|
971
|
+
* file layers the business rules on top (part-kind match, reference integrity,
|
|
972
|
+
* `position.anchorPartId` targets, value ranges, identity uniqueness).
|
|
973
|
+
* - loro-mirror's own `validateSchema` (run on every `setState`) only checks the
|
|
974
|
+
* storage structure, never these business invariants.
|
|
975
|
+
*
|
|
976
|
+
* Projection (`projection.ts`) calls `assertValidVideoDocument` before solving a
|
|
977
|
+
* document into the legacy `VideoDraft`.
|
|
978
|
+
*/
|
|
979
|
+
var VideoDocumentValidationError = class extends Error {
|
|
980
|
+
issues;
|
|
981
|
+
constructor(issues) {
|
|
982
|
+
super(`Invalid VideoDocument: ${issues.map((issue) => issue.message).join("; ")}`);
|
|
983
|
+
this.issues = issues;
|
|
984
|
+
this.name = "VideoDocumentValidationError";
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
/**
|
|
988
|
+
* Assert the document is legal enough to project. Only `error`-severity issues
|
|
989
|
+
* hard-reject; `recoverable` ones (dangling anchor / orphan, RFC 02 §11.1) are
|
|
990
|
+
* left for the projection to heal on read and do NOT throw. Use
|
|
991
|
+
* `validateVideoDocument` directly to inspect recoverable issues too.
|
|
992
|
+
*/
|
|
993
|
+
function assertValidVideoDocument(document) {
|
|
994
|
+
const blocking = validateVideoDocument(document).filter((issue) => (issue.severity ?? "error") === "error");
|
|
995
|
+
if (blocking.length > 0) throw new VideoDocumentValidationError(blocking);
|
|
996
|
+
}
|
|
997
|
+
function validateVideoDocument(document) {
|
|
998
|
+
const parsed = videoDocumentSchema.safeParse(document);
|
|
999
|
+
if (!parsed.success) return parsed.error.issues.map((issue) => ({
|
|
1000
|
+
code: "invalid_schema",
|
|
1001
|
+
path: zodPath(issue.path),
|
|
1002
|
+
message: issue.message
|
|
1003
|
+
}));
|
|
1004
|
+
const doc = parsed.data;
|
|
1005
|
+
const issues = [];
|
|
1006
|
+
const partLibrary = doc.part_library ?? {};
|
|
1007
|
+
for (const [partId, part] of Object.entries(partLibrary)) {
|
|
1008
|
+
if (!partUnionSchema.safeParse(part).success) {
|
|
1009
|
+
issues.push({
|
|
1010
|
+
code: "unknown_part_kind",
|
|
1011
|
+
path: `/part_library/${partId}`,
|
|
1012
|
+
message: `Part "${partId}" is not a supported part union`
|
|
1013
|
+
});
|
|
1014
|
+
continue;
|
|
1015
|
+
}
|
|
1016
|
+
const wrapperKind = getPartUnionKind(part);
|
|
1017
|
+
const innerKind = getPartKind(part);
|
|
1018
|
+
if (wrapperKind != null && innerKind != null && wrapperKind !== innerKind) issues.push({
|
|
1019
|
+
code: "part_kind_mismatch",
|
|
1020
|
+
path: `/part_library/${partId}`,
|
|
1021
|
+
message: `Part "${partId}" wrapper kind "${wrapperKind}" does not match inner kind "${innerKind}"`
|
|
1022
|
+
});
|
|
1023
|
+
validatePartValues(partId, part, issues);
|
|
1024
|
+
}
|
|
1025
|
+
for (const [idx, track] of (doc.tracks ?? []).entries()) validateTrack(track, `tracks/${idx}`, partLibrary, issues, track.parts_kind === "video_clip");
|
|
1026
|
+
validateSpeechCaptionReferences(partLibrary, issues);
|
|
1027
|
+
validatePositionReferences(doc, partLibrary, issues);
|
|
1028
|
+
return issues;
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* An `anchored` item whose `anchorPartId` no longer exists in the library is the
|
|
1032
|
+
* orphan condition (RFC 02 §4/§11.1) — e.g. a speech whose host video, or a
|
|
1033
|
+
* caption whose host speech, was concurrently deleted while this item was being
|
|
1034
|
+
* reparented in. This is flagged as a **recoverable** issue, NOT a hard error:
|
|
1035
|
+
* the projection heals it on read (the item's `fallback_abs_ms` snapshot seeds
|
|
1036
|
+
* its absolute position, then the cascade reassigns it to the nearest available
|
|
1037
|
+
* host, or it stays put as an absolute item). Reporting it here keeps the orphan
|
|
1038
|
+
* observable without blocking projection — the opposite of a hard reject, which
|
|
1039
|
+
* would make an inevitable concurrent-delete outcome un-projectable (§11.1).
|
|
1040
|
+
*/
|
|
1041
|
+
function validatePositionReferences(doc, partLibrary, issues) {
|
|
1042
|
+
const tracks = (doc.tracks ?? []).map((track, idx) => ({
|
|
1043
|
+
track,
|
|
1044
|
+
path: `tracks/${idx}`
|
|
1045
|
+
}));
|
|
1046
|
+
for (const { track, path } of tracks) for (const [idx, item] of (track?.items ?? []).entries()) {
|
|
1047
|
+
if (item.time_position.mode !== "anchored") continue;
|
|
1048
|
+
if (partLibrary[item.time_position.anchorPartId] == null) issues.push({
|
|
1049
|
+
code: "invalid_position_anchor",
|
|
1050
|
+
path: `/${path}/items/${idx}/time_position/anchorPartId`,
|
|
1051
|
+
message: `Track item "${path}/${idx}" anchors to missing part "${item.time_position.anchorPartId}"`,
|
|
1052
|
+
severity: "recoverable"
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
function validatePartValues(partId, part, issues) {
|
|
1057
|
+
const payload = part.video_clip ?? part.speech ?? part.caption ?? part.bgm ?? void 0;
|
|
1058
|
+
if (payload == null) return;
|
|
1059
|
+
if ("duration_ms" in payload && payload.duration_ms != null && payload.duration_ms < 0) issues.push({
|
|
1060
|
+
code: "invalid_part_value",
|
|
1061
|
+
path: `/part_library/${partId}/duration_ms`,
|
|
1062
|
+
message: `Part "${partId}" has negative duration_ms`
|
|
1063
|
+
});
|
|
1064
|
+
if ("volume" in payload && payload.volume != null && !Number.isFinite(payload.volume)) issues.push({
|
|
1065
|
+
code: "invalid_part_value",
|
|
1066
|
+
path: `/part_library/${partId}/volume`,
|
|
1067
|
+
message: `Part "${partId}" has non-finite volume`
|
|
1068
|
+
});
|
|
1069
|
+
if (part.video_clip != null) {
|
|
1070
|
+
const { play_in, play_out } = part.video_clip;
|
|
1071
|
+
if (play_in != null && play_in < 0) issues.push({
|
|
1072
|
+
code: "invalid_part_value",
|
|
1073
|
+
path: `/part_library/${partId}/play_in`,
|
|
1074
|
+
message: `Video clip "${partId}" has negative play_in`
|
|
1075
|
+
});
|
|
1076
|
+
if (play_out != null && play_in != null && play_out < play_in) issues.push({
|
|
1077
|
+
code: "invalid_part_value",
|
|
1078
|
+
path: `/part_library/${partId}/play_out`,
|
|
1079
|
+
message: `Video clip "${partId}" has play_out before play_in`
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
function validateTrack(track, path, partLibrary, issues, isMainTrack) {
|
|
1084
|
+
if (track == null) return;
|
|
1085
|
+
const seenPartIds = /* @__PURE__ */ new Set();
|
|
1086
|
+
for (const [idx, item] of (track.items ?? []).entries()) {
|
|
1087
|
+
const partId = item.part_id;
|
|
1088
|
+
if (partId != null && partId !== "") if (seenPartIds.has(partId)) issues.push({
|
|
1089
|
+
code: "duplicate_track_item_identity",
|
|
1090
|
+
path: `/${path}/items/${idx}/part_id`,
|
|
1091
|
+
message: `Track item "${path}/${idx}" reuses part_id "${partId}"`
|
|
1092
|
+
});
|
|
1093
|
+
else seenPartIds.add(partId);
|
|
1094
|
+
if (partId == null || partId === "") {
|
|
1095
|
+
issues.push({
|
|
1096
|
+
code: "missing_part_reference",
|
|
1097
|
+
path: `/${path}/items/${idx}/part_id`,
|
|
1098
|
+
message: `Track item "${path}/${idx}" has no part_id`
|
|
1099
|
+
});
|
|
1100
|
+
continue;
|
|
1101
|
+
}
|
|
1102
|
+
const part = partLibrary[partId];
|
|
1103
|
+
if (part == null) {
|
|
1104
|
+
issues.push({
|
|
1105
|
+
code: "missing_part_reference",
|
|
1106
|
+
path: `/${path}/items/${idx}/part_id`,
|
|
1107
|
+
message: `Track item "${path}/${idx}" references missing part "${partId}"`
|
|
1108
|
+
});
|
|
1109
|
+
continue;
|
|
1110
|
+
}
|
|
1111
|
+
const partKind = getPartKind(part);
|
|
1112
|
+
if (isMainTrack && partKind !== "video_clip") issues.push({
|
|
1113
|
+
code: "main_track_non_video_clip",
|
|
1114
|
+
path: `/${path}/items/${idx}/part_id`,
|
|
1115
|
+
message: `Main track item "${path}/${idx}" references non-video part "${partId}"`
|
|
1116
|
+
});
|
|
1117
|
+
if (track.parts_kind != null && partKind != null && track.parts_kind !== partKind) issues.push({
|
|
1118
|
+
code: "track_kind_mismatch",
|
|
1119
|
+
path: `/${path}/items/${idx}/part_id`,
|
|
1120
|
+
message: `Track "${path}" expects "${track.parts_kind}" but part "${partId}" is "${partKind}"`
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
/**
|
|
1125
|
+
* Check speech↔caption references. A reference to a *missing* part (speech's
|
|
1126
|
+
* caption_ids → deleted caption, or caption's speech_part_id → deleted speech)
|
|
1127
|
+
* is the orphan condition (RFC 02 §11.1): reported as **recoverable** so the
|
|
1128
|
+
* projection can heal it on read, not block. A *back-pointer mismatch* (caption
|
|
1129
|
+
* exists but does not point back) is data corruption, kept as a hard error.
|
|
1130
|
+
*/
|
|
1131
|
+
function validateSpeechCaptionReferences(partLibrary, issues) {
|
|
1132
|
+
for (const [partId, part] of Object.entries(partLibrary)) {
|
|
1133
|
+
if (part.speech != null) for (const captionId of part.speech.caption_ids ?? []) {
|
|
1134
|
+
const caption = partLibrary[captionId]?.caption;
|
|
1135
|
+
if (caption == null) {
|
|
1136
|
+
issues.push({
|
|
1137
|
+
code: "invalid_speech_caption_reference",
|
|
1138
|
+
path: `/part_library/${partId}/speech/caption_ids`,
|
|
1139
|
+
message: `Speech "${partId}" references missing caption "${captionId}"`,
|
|
1140
|
+
severity: "recoverable"
|
|
1141
|
+
});
|
|
1142
|
+
continue;
|
|
1143
|
+
}
|
|
1144
|
+
if (caption.speech_part_id !== partId) issues.push({
|
|
1145
|
+
code: "invalid_speech_caption_reference",
|
|
1146
|
+
path: `/part_library/${captionId}/caption/speech_part_id`,
|
|
1147
|
+
message: `Caption "${captionId}" does not point back to speech "${partId}"`
|
|
1148
|
+
});
|
|
1149
|
+
}
|
|
1150
|
+
if (part.caption != null) {
|
|
1151
|
+
const speechId = part.caption.speech_part_id;
|
|
1152
|
+
if (speechId != null && partLibrary[speechId]?.speech == null) issues.push({
|
|
1153
|
+
code: "invalid_speech_caption_reference",
|
|
1154
|
+
path: `/part_library/${partId}/caption/speech_part_id`,
|
|
1155
|
+
message: `Caption "${partId}" references missing speech "${speechId}"`,
|
|
1156
|
+
severity: "recoverable"
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
function getPartUnionKind(part) {
|
|
1162
|
+
if (part.video_clip != null) return "video_clip";
|
|
1163
|
+
if (part.speech != null) return "speech";
|
|
1164
|
+
if (part.caption != null) return "caption";
|
|
1165
|
+
if (part.bgm != null) return "bgm";
|
|
1166
|
+
}
|
|
1167
|
+
function getPartKind(part) {
|
|
1168
|
+
return part.video_clip?.kind ?? part.speech?.kind ?? part.caption?.kind ?? part.bgm?.kind;
|
|
1169
|
+
}
|
|
1170
|
+
function zodPath(path) {
|
|
1171
|
+
if (path.length === 0) return "/";
|
|
1172
|
+
return `/${path.map(String).join("/")}`;
|
|
1173
|
+
}
|
|
1174
|
+
//#endregion
|
|
1175
|
+
//#region src/document/projection.ts
|
|
1176
|
+
/**
|
|
1177
|
+
* Projection between the authoritative `VideoDocument` and the legacy
|
|
1178
|
+
* `VideoDraft` read-view (RFC 02 §5/§7). Both directions live here:
|
|
1179
|
+
*
|
|
1180
|
+
* - `toVideoDocument` ingests a `VideoDraft`, deriving each item's `position`
|
|
1181
|
+
* from the legacy absolute layout + aggregations; derived values (abs time,
|
|
1182
|
+
* `part_aggregations`, total duration) are dropped.
|
|
1183
|
+
* - `fromVideoDocument` solves a `VideoDocument` back into a `VideoDraft` via the
|
|
1184
|
+
* timeline-core cascade, re-deriving exactly those values.
|
|
1185
|
+
*
|
|
1186
|
+
* Business validation lives in `validation.ts`; `fromVideoDocument` asserts a
|
|
1187
|
+
* valid document before solving.
|
|
1188
|
+
*/
|
|
1189
|
+
/**
|
|
1190
|
+
* Ingest the legacy `VideoDraft` into the authoritative `VideoDocument`,
|
|
1191
|
+
* deriving each item's `time_position` from the legacy absolute layout +
|
|
1192
|
+
* aggregations (RFC 02 §4/§5). Absolute time, `part_aggregations`, and total
|
|
1193
|
+
* duration are dropped — they are re-derived by the projection.
|
|
1194
|
+
*/
|
|
1195
|
+
function toVideoDocument(draft) {
|
|
1196
|
+
const speechHost = buildSpeechHostMap(draft.part_aggregations);
|
|
1197
|
+
const partLibrary = draft.part_library ?? {};
|
|
1198
|
+
const toTrack = (track, isMain) => {
|
|
1199
|
+
if (track == null) return void 0;
|
|
1200
|
+
return {
|
|
1201
|
+
id: track.id,
|
|
1202
|
+
parts_kind: track.parts_kind,
|
|
1203
|
+
is_hidden: track.is_hidden,
|
|
1204
|
+
items: (track.items ?? []).map((item) => deriveItem(item, isMain, speechHost, partLibrary))
|
|
1205
|
+
};
|
|
1206
|
+
};
|
|
1207
|
+
const mainTrack = toTrack(draft.main_track, true);
|
|
1208
|
+
const tracks = [
|
|
1209
|
+
...(draft.above_main_tracks ?? []).map((t) => toTrack(t, false)),
|
|
1210
|
+
...mainTrack == null ? [] : [mainTrack],
|
|
1211
|
+
...(draft.below_main_tracks ?? []).map((t) => toTrack(t, false))
|
|
1212
|
+
];
|
|
1213
|
+
return {
|
|
1214
|
+
meta: {
|
|
1215
|
+
schema_version: VIDEO_DOCUMENT_SCHEMA_VERSION,
|
|
1216
|
+
draft_id: draft.id,
|
|
1217
|
+
project_id: draft.project_id,
|
|
1218
|
+
owner_id: draft.owner_id,
|
|
1219
|
+
thumbnail_storage_key: draft.thumbnail_storage_key,
|
|
1220
|
+
chat_session_id: draft.chat_session_id,
|
|
1221
|
+
video_creation_settings: clone(draft.video_creation_settings),
|
|
1222
|
+
version: draft.version
|
|
1223
|
+
},
|
|
1224
|
+
timeline: draft.timeline == null ? void 0 : { unit_time_ms: draft.timeline.unit_time_ms },
|
|
1225
|
+
tracks,
|
|
1226
|
+
part_library: toAuthoritativePartLibrary(draft.part_library)
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* Map the legacy `VideoDraft` part library into the authoritative shape
|
|
1231
|
+
* (reference/17 §5): the authoritative parts store no derived part-level
|
|
1232
|
+
* duration, so the read-view `duration_ms` is dropped from video / speech / bgm.
|
|
1233
|
+
* For a caption the legacy `duration_ms` is the generation-time length, stored
|
|
1234
|
+
* authoritatively as `initial_duration_ms`.
|
|
1235
|
+
*/
|
|
1236
|
+
function toAuthoritativePartLibrary(partLibrary) {
|
|
1237
|
+
if (partLibrary == null) return void 0;
|
|
1238
|
+
const out = {};
|
|
1239
|
+
for (const [partId, part] of Object.entries(partLibrary)) {
|
|
1240
|
+
if (typeof part !== "object" || part == null) continue;
|
|
1241
|
+
if (part.video_clip != null) out[partId] = { video_clip: withTrimWindowFromDuration(part.video_clip) };
|
|
1242
|
+
else if (part.speech != null) {
|
|
1243
|
+
const { duration_ms, rest } = splitDurationMs(part.speech);
|
|
1244
|
+
out[partId] = { speech: {
|
|
1245
|
+
...rest,
|
|
1246
|
+
media_duration_ms: rest.media_duration_ms ?? duration_ms
|
|
1247
|
+
} };
|
|
1248
|
+
} else if (part.caption != null) {
|
|
1249
|
+
const { duration_ms, rest } = splitDurationMs(part.caption);
|
|
1250
|
+
out[partId] = { caption: {
|
|
1251
|
+
...rest,
|
|
1252
|
+
initial_duration_ms: duration_ms
|
|
1253
|
+
} };
|
|
1254
|
+
} else if (part.bgm != null) out[partId] = { bgm: omitDurationMs(part.bgm) };
|
|
1255
|
+
}
|
|
1256
|
+
return out;
|
|
1257
|
+
}
|
|
1258
|
+
/**
|
|
1259
|
+
* Build a speech-host map from a part_aggregations list. Used by
|
|
1260
|
+
* `toVideoDocument` (legacy VideoDraft → VideoDocument) to recover each speech's
|
|
1261
|
+
* host video clip and relative offset. Aggregation items with null ids are
|
|
1262
|
+
* skipped (malformed input tolerance).
|
|
1263
|
+
*/
|
|
1264
|
+
function buildSpeechHostMap(aggregations) {
|
|
1265
|
+
const map = /* @__PURE__ */ new Map();
|
|
1266
|
+
for (const aggregation of aggregations ?? []) {
|
|
1267
|
+
const host = aggregation.body_part_id;
|
|
1268
|
+
if (host == null) continue;
|
|
1269
|
+
for (const attachment of aggregation.attachments ?? []) {
|
|
1270
|
+
if (attachment.part_id == null) continue;
|
|
1271
|
+
map.set(attachment.part_id, {
|
|
1272
|
+
hostPartId: host,
|
|
1273
|
+
offsetMs: Math.round(attachment.relative_time_position ?? 0)
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
return map;
|
|
1278
|
+
}
|
|
1279
|
+
/**
|
|
1280
|
+
* Derive `time_position` (and `fallbackAbsMs` for anchored items) from a legacy
|
|
1281
|
+
* absolute time + pre-built speech-host map + part library. The three-branch
|
|
1282
|
+
* rule (RFC 02 §4/§5, reference/17 §3):
|
|
1283
|
+
*
|
|
1284
|
+
* 1. main-track → `sequential`
|
|
1285
|
+
* 2. speech/attachment (part_id in speechHost) → `anchored(host, offsetMs)`
|
|
1286
|
+
* 3. caption (part has `speech_part_id`) → `anchored(speech, start_ms)`
|
|
1287
|
+
* 4. everything else → `absolute(abs)`
|
|
1288
|
+
*
|
|
1289
|
+
* `partLibrary` values may be `PartUnion | string | undefined` (draft raw
|
|
1290
|
+
* form); only object-typed entries are inspected for `caption`.
|
|
1291
|
+
*/
|
|
1292
|
+
function derivePositionFromAbs(partId, abs, isMain, speechHost, partLibrary) {
|
|
1293
|
+
if (isMain) return { timePosition: { mode: "sequential" } };
|
|
1294
|
+
const host = speechHost.get(partId);
|
|
1295
|
+
if (host != null) return {
|
|
1296
|
+
timePosition: {
|
|
1297
|
+
mode: "anchored",
|
|
1298
|
+
anchorPartId: host.hostPartId,
|
|
1299
|
+
offsetMs: host.offsetMs
|
|
1300
|
+
},
|
|
1301
|
+
fallbackAbsMs: abs
|
|
1302
|
+
};
|
|
1303
|
+
const part = partLibrary[partId];
|
|
1304
|
+
const captionPart = typeof part === "object" && part != null ? part.caption : void 0;
|
|
1305
|
+
const speechId = captionPart?.speech_part_id ?? void 0;
|
|
1306
|
+
if (speechId != null) return {
|
|
1307
|
+
timePosition: {
|
|
1308
|
+
mode: "anchored",
|
|
1309
|
+
anchorPartId: speechId,
|
|
1310
|
+
offsetMs: Math.round(captionPart?.start_ms ?? 0)
|
|
1311
|
+
},
|
|
1312
|
+
fallbackAbsMs: abs
|
|
1313
|
+
};
|
|
1314
|
+
return { timePosition: {
|
|
1315
|
+
mode: "absolute",
|
|
1316
|
+
offsetMs: abs
|
|
1317
|
+
} };
|
|
1318
|
+
}
|
|
1319
|
+
/** Derive one item's authoritative `time_position` from its legacy abs + aggregation. */
|
|
1320
|
+
function deriveItem(item, isMain, speechHost, partLibrary) {
|
|
1321
|
+
const partId = item.part_id ?? "";
|
|
1322
|
+
const { timePosition, fallbackAbsMs } = derivePositionFromAbs(partId, Math.round(item.abs_time_position ?? 0), isMain, speechHost, partLibrary);
|
|
1323
|
+
return fallbackAbsMs == null ? {
|
|
1324
|
+
part_id: partId,
|
|
1325
|
+
time_position: timePosition
|
|
1326
|
+
} : {
|
|
1327
|
+
part_id: partId,
|
|
1328
|
+
time_position: timePosition,
|
|
1329
|
+
fallback_abs_ms: fallbackAbsMs
|
|
1330
|
+
};
|
|
1331
|
+
}
|
|
1332
|
+
/**
|
|
1333
|
+
* Project the authoritative `VideoDocument` back into the legacy `VideoDraft`
|
|
1334
|
+
* read-view, solving each item's absolute position, the `part_aggregations`, and
|
|
1335
|
+
* the total duration via the timeline-core cascade.
|
|
1336
|
+
*/
|
|
1337
|
+
function fromVideoDocument(document) {
|
|
1338
|
+
assertValidVideoDocument(document);
|
|
1339
|
+
const view = solveVideoDocument(document);
|
|
1340
|
+
const tracks = document.tracks ?? [];
|
|
1341
|
+
const mainTrack = tracks.find((t) => t.parts_kind === "video_clip");
|
|
1342
|
+
const aboveTracks = tracks.filter((t) => t.parts_kind === "caption");
|
|
1343
|
+
const belowTracks = tracks.filter((t) => t.parts_kind === "speech" || t.parts_kind === "bgm");
|
|
1344
|
+
return {
|
|
1345
|
+
id: document.meta.draft_id,
|
|
1346
|
+
project_id: document.meta.project_id,
|
|
1347
|
+
owner_id: document.meta.owner_id,
|
|
1348
|
+
thumbnail_storage_key: document.meta.thumbnail_storage_key,
|
|
1349
|
+
timeline: {
|
|
1350
|
+
duration_ms: view.durationMs,
|
|
1351
|
+
unit_time_ms: document.timeline?.unit_time_ms
|
|
1352
|
+
},
|
|
1353
|
+
video_creation_settings: clone(document.meta.video_creation_settings),
|
|
1354
|
+
chat_session_id: document.meta.chat_session_id,
|
|
1355
|
+
main_track: draftTrack(mainTrack, view.absByPartId),
|
|
1356
|
+
above_main_tracks: aboveTracks.map((t) => draftTrack(t, view.absByPartId)),
|
|
1357
|
+
below_main_tracks: belowTracks.map((t) => draftTrack(t, view.absByPartId)),
|
|
1358
|
+
part_aggregations: view.aggregations,
|
|
1359
|
+
part_library: toReadViewPartLibrary(view.partLibrary, view.durationMs),
|
|
1360
|
+
version: document.meta.version
|
|
1361
|
+
};
|
|
1362
|
+
}
|
|
1363
|
+
/**
|
|
1364
|
+
* Map the authoritative part library into the `VideoDraft` read-view shape,
|
|
1365
|
+
* re-injecting the derived effective `duration_ms` downstream expects
|
|
1366
|
+
* (reference/17 §5/§7) — the authoritative parts store no part-level duration:
|
|
1367
|
+
*
|
|
1368
|
+
* - video clip → `(play_out - play_in) / speed` (`effectiveVideoClipDurationMs`)
|
|
1369
|
+
* - speech → its intrinsic `media_duration_ms` (no trim/speed)
|
|
1370
|
+
* - caption → its generation-time `initial_duration_ms`
|
|
1371
|
+
* - bgm → the timeline total (`durationMs`)
|
|
1372
|
+
*
|
|
1373
|
+
* Parts are deep-cloned; the engine extensions (`media_duration_ms`,
|
|
1374
|
+
* `initial_duration_ms`) are kept alongside the injected `duration_ms`.
|
|
1375
|
+
*/
|
|
1376
|
+
function toReadViewPartLibrary(partLibrary, durationMs) {
|
|
1377
|
+
const out = {};
|
|
1378
|
+
for (const [partId, part] of Object.entries(partLibrary)) if (part.video_clip != null) {
|
|
1379
|
+
const clip = clone(part.video_clip);
|
|
1380
|
+
out[partId] = { video_clip: {
|
|
1381
|
+
...clip,
|
|
1382
|
+
duration_ms: effectiveVideoClipDurationMs(clip)
|
|
1383
|
+
} };
|
|
1384
|
+
} else if (part.speech != null) {
|
|
1385
|
+
const speech = clone(part.speech);
|
|
1386
|
+
out[partId] = { speech: {
|
|
1387
|
+
...speech,
|
|
1388
|
+
duration_ms: speech.media_duration_ms
|
|
1389
|
+
} };
|
|
1390
|
+
} else if (part.caption != null) {
|
|
1391
|
+
const caption = clone(part.caption);
|
|
1392
|
+
out[partId] = { caption: {
|
|
1393
|
+
...caption,
|
|
1394
|
+
duration_ms: caption.initial_duration_ms
|
|
1395
|
+
} };
|
|
1396
|
+
} else if (part.bgm != null) out[partId] = { bgm: {
|
|
1397
|
+
...clone(part.bgm),
|
|
1398
|
+
duration_ms: durationMs
|
|
1399
|
+
} };
|
|
1400
|
+
return out;
|
|
1401
|
+
}
|
|
1402
|
+
/** Deep-clone a part payload, dropping the derived read-view `duration_ms`. */
|
|
1403
|
+
function omitDurationMs(part) {
|
|
1404
|
+
const { duration_ms: _drop, ...rest } = clone(part);
|
|
1405
|
+
return rest;
|
|
1406
|
+
}
|
|
1407
|
+
/**
|
|
1408
|
+
* Migrate a legacy video clip into the authoritative trim-window-only shape: the
|
|
1409
|
+
* authoritative part stores no `duration_ms`, so it is dropped — but a legacy
|
|
1410
|
+
* clip with no explicit trim window (`play_in` / `play_out` absent) would then
|
|
1411
|
+
* compute an effective length of 0 and vanish from the layout. When that clip
|
|
1412
|
+
* carried a legacy `duration_ms`, backfill it as the trim window (`play_in` 0,
|
|
1413
|
+
* `play_out = duration_ms`, no speed) so the effective length is preserved, then
|
|
1414
|
+
* drop `duration_ms`. A clip that already has a trim window keeps it verbatim.
|
|
1415
|
+
*/
|
|
1416
|
+
function withTrimWindowFromDuration(clip) {
|
|
1417
|
+
const stripped = omitDurationMs(clip);
|
|
1418
|
+
if (clip.play_in != null || clip.play_out != null) return stripped;
|
|
1419
|
+
const legacyDuration = clip.duration_ms;
|
|
1420
|
+
if (legacyDuration == null || !Number.isFinite(legacyDuration)) return stripped;
|
|
1421
|
+
return {
|
|
1422
|
+
...stripped,
|
|
1423
|
+
play_in: 0,
|
|
1424
|
+
play_out: legacyDuration
|
|
1425
|
+
};
|
|
1426
|
+
}
|
|
1427
|
+
/** Deep-clone a part payload, returning its `duration_ms` separately from the rest. */
|
|
1428
|
+
function splitDurationMs(part) {
|
|
1429
|
+
const { duration_ms, ...rest } = clone(part);
|
|
1430
|
+
return {
|
|
1431
|
+
duration_ms,
|
|
1432
|
+
rest
|
|
1433
|
+
};
|
|
1434
|
+
}
|
|
1435
|
+
function draftTrack(track, absByPartId) {
|
|
1436
|
+
if (track == null) return void 0;
|
|
1437
|
+
return {
|
|
1438
|
+
id: track.id,
|
|
1439
|
+
parts_kind: track.parts_kind,
|
|
1440
|
+
is_hidden: track.is_hidden,
|
|
1441
|
+
items: (track.items ?? []).map((item) => ({
|
|
1442
|
+
part_id: item.part_id,
|
|
1443
|
+
abs_time_position: absByPartId.get(item.part_id) ?? 0
|
|
1444
|
+
}))
|
|
1445
|
+
};
|
|
1446
|
+
}
|
|
1447
|
+
function clone(value) {
|
|
1448
|
+
if (Array.isArray(value)) return value.map((item) => clone(item));
|
|
1449
|
+
if (value != null && typeof value === "object") {
|
|
1450
|
+
const result = {};
|
|
1451
|
+
for (const [key, child] of Object.entries(value)) result[key] = clone(child);
|
|
1452
|
+
return result;
|
|
1453
|
+
}
|
|
1454
|
+
return value;
|
|
1455
|
+
}
|
|
1456
|
+
//#endregion
|
|
1457
|
+
//#region src/document/mirror-read.ts
|
|
1458
|
+
/**
|
|
1459
|
+
* Project the mirror state (`VideoDocumentDraft`) into the authoritative
|
|
1460
|
+
* `VideoDocument`. The storage shape is isomorphic to the domain shape (RFC 03
|
|
1461
|
+
* §4, reference/17 §4: meta map + a single `tracks` list + part_library), so this
|
|
1462
|
+
* is a near-identity — it reads `time_position` / `fallback_abs_ms` JSON blobs
|
|
1463
|
+
* back into structured values and trims empty strings, nothing more.
|
|
1464
|
+
*
|
|
1465
|
+
* It maps only authoritative facts (RFC 02 §6): `part_id` + `time_position`. The
|
|
1466
|
+
* projection-derived `VideoDraft` read-view (absolute time, `part_aggregations`,
|
|
1467
|
+
* total duration) is solved separately by the cascade, not here.
|
|
1468
|
+
*
|
|
1469
|
+
* It reads in-memory mirror state (O(n) over the document), not a Loro
|
|
1470
|
+
* `toJSON()` FFI rebuild — the cost the prior schema adapter paid on every
|
|
1471
|
+
* `snapshot()`.
|
|
1472
|
+
*/
|
|
1473
|
+
function readVideoDocumentFromDraft(draft) {
|
|
1474
|
+
const partLibrary = {};
|
|
1475
|
+
for (const [partId, payload] of recordEntries(draft.part_library)) {
|
|
1476
|
+
const part = draftToPartUnion(payload);
|
|
1477
|
+
if (part != null) partLibrary[partId] = part;
|
|
1478
|
+
}
|
|
1479
|
+
const timeline = draft.timeline;
|
|
1480
|
+
const hasTimeline = timeline?.unit_time_ms != null;
|
|
1481
|
+
const meta = draft.meta;
|
|
1482
|
+
return {
|
|
1483
|
+
meta: {
|
|
1484
|
+
schema_version: VIDEO_DOCUMENT_SCHEMA_VERSION,
|
|
1485
|
+
draft_id: emptyToUndefined(meta?.draft_id),
|
|
1486
|
+
project_id: emptyToUndefined(meta?.project_id),
|
|
1487
|
+
owner_id: emptyToUndefined(meta?.owner_id),
|
|
1488
|
+
thumbnail_storage_key: emptyToUndefined(meta?.thumbnail_storage_key),
|
|
1489
|
+
chat_session_id: emptyToUndefined(meta?.chat_session_id),
|
|
1490
|
+
video_creation_settings: meta?.video_creation_settings ?? void 0,
|
|
1491
|
+
version: meta?.version ?? void 0
|
|
1492
|
+
},
|
|
1493
|
+
timeline: hasTimeline ? { unit_time_ms: timeline.unit_time_ms } : void 0,
|
|
1494
|
+
tracks: rowsToTracks(draft.tracks),
|
|
1495
|
+
part_library: partLibrary
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
/** Map a movable-list of track rows (`$cid`-bearing) into domain tracks, dropping empty-id placeholders. */
|
|
1499
|
+
function rowsToTracks(rows) {
|
|
1500
|
+
return (rows ?? []).filter((row) => row.id != null && row.id !== "").map(rowToTrack);
|
|
1501
|
+
}
|
|
1502
|
+
function rowToTrack(row) {
|
|
1503
|
+
return {
|
|
1504
|
+
id: row.id,
|
|
1505
|
+
parts_kind: row.parts_kind ?? void 0,
|
|
1506
|
+
is_hidden: row.is_hidden ?? void 0,
|
|
1507
|
+
items: (row.items ?? []).map((item) => {
|
|
1508
|
+
const result = {
|
|
1509
|
+
part_id: item.part_id ?? "",
|
|
1510
|
+
time_position: item.time_position
|
|
1511
|
+
};
|
|
1512
|
+
if (item.fallback_abs_ms != null) result.fallback_abs_ms = item.fallback_abs_ms;
|
|
1513
|
+
return result;
|
|
1514
|
+
})
|
|
1515
|
+
};
|
|
1516
|
+
}
|
|
1517
|
+
function emptyToUndefined(value) {
|
|
1518
|
+
return value == null || value === "" ? void 0 : value;
|
|
1519
|
+
}
|
|
1520
|
+
//#endregion
|
|
1521
|
+
//#region src/document/mirror-adapter.ts
|
|
1522
|
+
/**
|
|
1523
|
+
* Storage-layer adapter that backs a `VideoDocument` with `loro-mirror` (ADR
|
|
1524
|
+
* 0008). The mirror holds an in-memory immutable state synced to the `LoroDoc`
|
|
1525
|
+
* by declarative diff, replacing the hand-rolled `@mengine/schema` adapter:
|
|
1526
|
+
*
|
|
1527
|
+
* - `snapshot()` projects the mirror's in-memory state to the read model — no
|
|
1528
|
+
* per-call `toJSON()` FFI rebuild.
|
|
1529
|
+
* - `transact(edit, audit)` runs the whole op in one `mirror.setState` callback:
|
|
1530
|
+
* one diff, one `doc.commit` carrying the audit message. The callback edits an
|
|
1531
|
+
* immer draft, so a throw inside it discards the draft and never touches Loro
|
|
1532
|
+
* (natural rollback) — no `guard` / `rollback` / `openTransaction` machinery.
|
|
1533
|
+
* - mirror's `idSelector` (track items keyed by `part_id`) diffs reorders to
|
|
1534
|
+
* real Loro `move` ops on every lane, so per-item CRDT identity survives on
|
|
1535
|
+
* main and secondary tracks alike.
|
|
1536
|
+
*/
|
|
1537
|
+
var MirrorVideoDocumentAdapter = class {
|
|
1538
|
+
doc;
|
|
1539
|
+
mirror;
|
|
1540
|
+
constructor(doc) {
|
|
1541
|
+
this.doc = doc;
|
|
1542
|
+
this.mirror = new Mirror({
|
|
1543
|
+
doc,
|
|
1544
|
+
schema: videoDocumentMirrorSchema
|
|
1545
|
+
});
|
|
1546
|
+
}
|
|
1547
|
+
snapshot() {
|
|
1548
|
+
return readVideoDocumentFromDraft(this.mirror.getState());
|
|
1549
|
+
}
|
|
1550
|
+
/**
|
|
1551
|
+
* True once the doc holds real document content. A fresh mirror over an empty
|
|
1552
|
+
* doc still reports defaulted root maps, so probe the stored `schema_version`
|
|
1553
|
+
* (empty until a snapshot is bootstrapped or synced in).
|
|
1554
|
+
*/
|
|
1555
|
+
hasContent() {
|
|
1556
|
+
return (this.mirror.getState().meta?.schema_version ?? "") !== "";
|
|
1557
|
+
}
|
|
1558
|
+
/**
|
|
1559
|
+
* Apply one op as a single transaction. `edit` mutates the immer draft; mirror
|
|
1560
|
+
* diffs the result and commits once with the audit `message`. A throw in `edit`
|
|
1561
|
+
* discards the draft (Loro untouched). When `edit` produces no change, mirror
|
|
1562
|
+
* skips the commit — matching the prior "empty op leaves no audit" behavior.
|
|
1563
|
+
*/
|
|
1564
|
+
transact(edit, audit) {
|
|
1565
|
+
this.mirror.setState((draft) => {
|
|
1566
|
+
edit(draft);
|
|
1567
|
+
}, {
|
|
1568
|
+
origin: "mengine.semantic_editor",
|
|
1569
|
+
message: JSON.stringify({
|
|
1570
|
+
semantic_op: audit.kind,
|
|
1571
|
+
payload: audit.payload,
|
|
1572
|
+
intent: audit.intent ?? null
|
|
1573
|
+
})
|
|
1574
|
+
});
|
|
1575
|
+
}
|
|
1576
|
+
};
|
|
1577
|
+
/** Build a fresh Loro doc seeded with `document` through the mirror. */
|
|
1578
|
+
function createMirrorVideoDocument(document, options = {}) {
|
|
1579
|
+
assertValidVideoDocument(document);
|
|
1580
|
+
const doc = new LoroDoc();
|
|
1581
|
+
if (options.peerId != null) doc.setPeerId(options.peerId);
|
|
1582
|
+
new Mirror({
|
|
1583
|
+
doc,
|
|
1584
|
+
schema: videoDocumentMirrorSchema
|
|
1585
|
+
}).setState((draft) => {
|
|
1586
|
+
seedDraft(draft, document);
|
|
1587
|
+
}, { origin: options.origin ?? "mengine.bootstrap" });
|
|
1588
|
+
return doc;
|
|
1589
|
+
}
|
|
1590
|
+
/** Build a `MirrorVideoDocumentAdapter` over a fresh doc seeded with `document`. */
|
|
1591
|
+
function createMirrorVideoDocumentAdapter(document, options) {
|
|
1592
|
+
return new MirrorVideoDocumentAdapter(createMirrorVideoDocument(document, options));
|
|
1593
|
+
}
|
|
1594
|
+
/** Write a whole `VideoDocument` into the mirror draft (used to seed a fresh doc). */
|
|
1595
|
+
function seedDraft(draft, document) {
|
|
1596
|
+
draft.meta = {
|
|
1597
|
+
schema_version: document.meta.schema_version,
|
|
1598
|
+
draft_id: document.meta.draft_id ?? void 0,
|
|
1599
|
+
project_id: document.meta.project_id ?? void 0,
|
|
1600
|
+
owner_id: document.meta.owner_id ?? void 0,
|
|
1601
|
+
thumbnail_storage_key: document.meta.thumbnail_storage_key ?? void 0,
|
|
1602
|
+
chat_session_id: document.meta.chat_session_id ?? void 0,
|
|
1603
|
+
video_creation_settings: document.meta.video_creation_settings ?? void 0,
|
|
1604
|
+
version: document.meta.version ?? void 0
|
|
1605
|
+
};
|
|
1606
|
+
draft.timeline = { unit_time_ms: document.timeline?.unit_time_ms ?? void 0 };
|
|
1607
|
+
draft.tracks = (document.tracks ?? []).map(toTrackRow);
|
|
1608
|
+
draft.part_library = {};
|
|
1609
|
+
for (const [partId, part] of Object.entries(document.part_library ?? {})) draft.part_library[partId] = partUnionToDraft(part);
|
|
1610
|
+
}
|
|
1611
|
+
/** Map a domain `Track` to a draft track row (no lane/lane_order — see schema). */
|
|
1612
|
+
function toTrackRow(track) {
|
|
1613
|
+
return {
|
|
1614
|
+
id: track.id ?? "",
|
|
1615
|
+
parts_kind: track.parts_kind ?? void 0,
|
|
1616
|
+
is_hidden: track.is_hidden ?? void 0,
|
|
1617
|
+
items: (track.items ?? []).map((item) => ({
|
|
1618
|
+
part_id: item.part_id,
|
|
1619
|
+
time_position: item.time_position,
|
|
1620
|
+
fallback_abs_ms: item.fallback_abs_ms
|
|
1621
|
+
}))
|
|
1622
|
+
};
|
|
1623
|
+
}
|
|
1624
|
+
//#endregion
|
|
1625
|
+
//#region src/editor/id-gen.ts
|
|
1626
|
+
/**
|
|
1627
|
+
* Part-id generation, aligned with the online ecosystem.
|
|
1628
|
+
*
|
|
1629
|
+
* The authoritative online producers — agent-harness (`@harness/shared`
|
|
1630
|
+
* `genObjId`) and director.v2 (`common/obj_id.py` `gen_obj_id`) — both mint part
|
|
1631
|
+
* ids as `` `${prefix}_${ulid()}` ``, and real captured drafts use exactly that
|
|
1632
|
+
* shape (`clip_…` / `spe_…` / `cap_…` / `bgm_…`, each a 26-char ULID). The engine
|
|
1633
|
+
* previously emitted `vc_<base36 timestamp><6 random>`, a different prefix AND a
|
|
1634
|
+
* different encoding — the sole cross-repo id divergence. This module removes it
|
|
1635
|
+
* by emitting the same `<prefix>_<ULID>` bytes.
|
|
1636
|
+
*
|
|
1637
|
+
* The ULID is generated inline (Crockford Base32, 48-bit time + 80-bit random)
|
|
1638
|
+
* rather than pulling the `ulid` npm package: the randomness class matches the
|
|
1639
|
+
* old generator (both `Math.random`-based) and it keeps `@mengine/medeo-client`
|
|
1640
|
+
* dependency-free for a purely mechanical id string. Part ids only need to be
|
|
1641
|
+
* unique and lexicographically time-sortable, which this satisfies.
|
|
1642
|
+
*/
|
|
1643
|
+
/** Crockford Base32 alphabet (no I, L, O, U), per the ULID spec. */
|
|
1644
|
+
const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
1645
|
+
const TIME_LEN = 10;
|
|
1646
|
+
const RANDOM_LEN = 16;
|
|
1647
|
+
function encodeTime(now) {
|
|
1648
|
+
let out = "";
|
|
1649
|
+
let ms = now;
|
|
1650
|
+
for (let i = TIME_LEN - 1; i >= 0; i--) {
|
|
1651
|
+
const mod = ms % 32;
|
|
1652
|
+
out = CROCKFORD[mod] + out;
|
|
1653
|
+
ms = (ms - mod) / 32;
|
|
1654
|
+
}
|
|
1655
|
+
return out;
|
|
1656
|
+
}
|
|
1657
|
+
function encodeRandom() {
|
|
1658
|
+
let out = "";
|
|
1659
|
+
for (let i = 0; i < RANDOM_LEN; i++) out += CROCKFORD[Math.floor(Math.random() * 32)];
|
|
1660
|
+
return out;
|
|
1661
|
+
}
|
|
1662
|
+
/** A 26-char Crockford Base32 ULID (10-char time + 16-char random). */
|
|
1663
|
+
function ulid() {
|
|
1664
|
+
return encodeTime(Date.now()) + encodeRandom();
|
|
1665
|
+
}
|
|
1666
|
+
function generatePartId(prefix) {
|
|
1667
|
+
return `${prefix}_${ulid()}`;
|
|
1668
|
+
}
|
|
1669
|
+
//#endregion
|
|
1670
|
+
//#region src/editor/schemas/shared.ts
|
|
1671
|
+
const clipIdSchema = z.string().min(1).describe("The clip part ID on the timeline");
|
|
1672
|
+
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)");
|
|
1673
|
+
const mediaIdSchema = z.string().min(1).describe("The media asset ID");
|
|
1674
|
+
const speechIdSchema = z.string().min(1).describe("The speech part ID on the timeline");
|
|
1675
|
+
const timelineMsSchema = z.number().int().min(0).describe("Time position in milliseconds on the timeline (>= 0)");
|
|
1676
|
+
const positiveMsSchema = z.number().int().positive().describe("Duration in milliseconds (> 0)");
|
|
1677
|
+
const volumeSchema = z.number().min(-60).max(20).describe("Volume in decibels (-60.0 to 20.0; 0.0 = original, -60 = mute, +20 = max)");
|
|
1678
|
+
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)");
|
|
1679
|
+
const tangentHandleSchema = z.object({
|
|
1680
|
+
x: z.number().finite(),
|
|
1681
|
+
y: z.number().finite()
|
|
1682
|
+
}).describe("Bezier tangent handle (x, y)");
|
|
1683
|
+
const speedKeyframeSchema = z.object({
|
|
1684
|
+
position: z.number().min(0).max(1),
|
|
1685
|
+
rate: z.number().min(0),
|
|
1686
|
+
in_tangent: tangentHandleSchema.optional(),
|
|
1687
|
+
out_tangent: tangentHandleSchema.optional()
|
|
1688
|
+
}).describe("A speed keyframe: normalized position (0..1), rate, optional tangents");
|
|
1689
|
+
/**
|
|
1690
|
+
* A clip's playback-speed fact, the only thing `SetVideoClipSpeedShift` writes.
|
|
1691
|
+
* Mirrors the IDL `SpeedShift`: `category` is `linear` | `curve`, and `config`
|
|
1692
|
+
* is a discriminated union — `{ linear: { speed } }` for a constant multiplier
|
|
1693
|
+
* (the multiplier projection reads at `config.linear.speed`) or `{ curve: {
|
|
1694
|
+
* keyframes } }` for a Bezier-controlled variable speed (RFC 02 / `reference/16`
|
|
1695
|
+
* §0). Exactly one of `linear` / `curve` is present.
|
|
1696
|
+
*/
|
|
1697
|
+
const speedShiftSchema = z.object({
|
|
1698
|
+
category: z.enum(["linear", "curve"]),
|
|
1699
|
+
mode: z.string(),
|
|
1700
|
+
config: z.union([z.object({ linear: z.object({ speed: z.number().finite().positive() }) }), z.object({ curve: z.object({ keyframes: z.array(speedKeyframeSchema).min(2) }) })])
|
|
1701
|
+
}).describe("Speed shift: linear multiplier or Bezier curve, mirroring the IDL shape");
|
|
1702
|
+
const voiceSchema = z.object({
|
|
1703
|
+
id: z.string().min(1),
|
|
1704
|
+
name: z.string()
|
|
1705
|
+
}).describe("TTS voice summary attached to a speech");
|
|
1706
|
+
//#endregion
|
|
1707
|
+
//#region src/editor/schemas/speech-assets.ts
|
|
1708
|
+
/**
|
|
1709
|
+
* The materialized TTS result shared by `AddSpeeches` / `ChangeSpeechScript` /
|
|
1710
|
+
* `ChangeSpeechVoice` (see `results/phase-4-side-effect-payload-contract.md`
|
|
1711
|
+
* §1/§2). The side effect (TTS/ASR + billing) runs upstream; the op receives the
|
|
1712
|
+
* stable speech + caption parts and writes them as authoritative facts. No
|
|
1713
|
+
* cascade runs on write — the projection derives absolute positions on read.
|
|
1714
|
+
*
|
|
1715
|
+
* Each speech carries the anchoring fact directly (RFC 02 §4): the host video
|
|
1716
|
+
* clip `anchor_part_id` and the `offset_ms` within it. The upstream caller
|
|
1717
|
+
* already knows which clip a speech attaches to, so the op writes
|
|
1718
|
+
* `{ mode:'anchored', anchorPartId, offsetMs }` verbatim — no write-time
|
|
1719
|
+
* host-picking. Captions anchor to their speech via the caption part's
|
|
1720
|
+
* `start_ms` (offset within the speech).
|
|
1721
|
+
*/
|
|
1722
|
+
const speechAssetSchema = z.object({
|
|
1723
|
+
speech_id: speechIdSchema.describe("The speech part ID (= side-effect speech_parts[].id)"),
|
|
1724
|
+
anchor_part_id: clipIdSchema.describe("Host video clip part ID the speech anchors to (RFC 02 §4)"),
|
|
1725
|
+
offset_ms: timelineMsSchema.describe("Offset within the host clip (speech.abs = host.abs + offset_ms)"),
|
|
1726
|
+
audio_storage_key: z.string().min(1),
|
|
1727
|
+
duration_ms: positiveMsSchema,
|
|
1728
|
+
audio_script: z.string(),
|
|
1729
|
+
volume: volumeSchema,
|
|
1730
|
+
voice: voiceSchema,
|
|
1731
|
+
origin_speech_id: z.string().min(1),
|
|
1732
|
+
caption_ids: z.array(z.string().min(1)).describe("Caption part IDs owned by this speech")
|
|
1733
|
+
});
|
|
1734
|
+
const captionAssetSchema = z.object({
|
|
1735
|
+
caption_id: z.string().min(1).describe("The caption part ID (= side-effect created_caption_parts[].id)"),
|
|
1736
|
+
speech_part_id: speechIdSchema.describe("The owning speech part ID"),
|
|
1737
|
+
text: z.string(),
|
|
1738
|
+
start_ms: timelineMsSchema.describe("Offset within the host speech (caption.abs = speech.abs + start_ms)"),
|
|
1739
|
+
duration_ms: positiveMsSchema
|
|
1740
|
+
});
|
|
1741
|
+
/** A materialized speech-subtree write (speeches + their captions). */
|
|
1742
|
+
const speechAssetsSchema = z.object({
|
|
1743
|
+
speeches: z.array(speechAssetSchema).min(1).describe("Materialized speech parts to write"),
|
|
1744
|
+
captions: z.array(captionAssetSchema).describe("Materialized caption parts owned by the speeches")
|
|
1745
|
+
});
|
|
1746
|
+
//#endregion
|
|
1747
|
+
//#region src/editor/schemas/add-speeches.ts
|
|
1748
|
+
/**
|
|
1749
|
+
* Add speeches (and their captions). TTS runs upstream; the stable speech /
|
|
1750
|
+
* caption parts arrive materialized (see `speech-assets.ts`). The op writes the
|
|
1751
|
+
* parts and each speech's `{ mode:'anchored', anchorPartId, offsetMs }` fact
|
|
1752
|
+
* verbatim — no write-time host-picking, no cascade (RFC 02 §4). The projection
|
|
1753
|
+
* derives absolute positions on read.
|
|
1754
|
+
*/
|
|
1755
|
+
const addSpeechesInputSchema = speechAssetsSchema.describe("Materialized speeches + captions to add");
|
|
1756
|
+
//#endregion
|
|
1757
|
+
//#region src/editor/schemas/add-video-clips.ts
|
|
1758
|
+
/**
|
|
1759
|
+
* Add video clips to a track. Each clip's duration facts are separated so a
|
|
1760
|
+
* single number is never overloaded (RFC 02 / `reference/16` §0b):
|
|
1761
|
+
*
|
|
1762
|
+
* - `media_duration_ms` is the source media's intrinsic full length (a resource
|
|
1763
|
+
* fact, written to the part);
|
|
1764
|
+
* - `play_in` / `play_out` are the optional trim window into that media; when
|
|
1765
|
+
* omitted the whole media is used (`play_in=0`, `play_out=media_duration_ms`).
|
|
1766
|
+
*
|
|
1767
|
+
* The clip's effective timeline duration is derived by the projection from the
|
|
1768
|
+
* trim window and `speed_shift` — it is never an input here.
|
|
1769
|
+
*/
|
|
1770
|
+
const addVideoClipsInputSchema = z.object({
|
|
1771
|
+
clips: z.array(z.object({
|
|
1772
|
+
media_id: mediaIdSchema.describe("The media asset ID for the video clip"),
|
|
1773
|
+
start_ms: timelineMsSchema.optional().describe("Absolute start time in milliseconds on the timeline"),
|
|
1774
|
+
media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
|
|
1775
|
+
play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
|
|
1776
|
+
play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)"),
|
|
1777
|
+
track_id: z.string().min(1).optional().describe("Target track ID (optional, defaults to main track)")
|
|
1778
|
+
})).min(1).describe("List of video clips to create"),
|
|
1779
|
+
before_clip_id: z.string().min(1).optional().describe("Insert new clips before this clip ID"),
|
|
1780
|
+
after_clip_id: z.string().min(1).optional().describe("Insert new clips after this clip ID")
|
|
1781
|
+
}).superRefine((data, ctx) => {
|
|
1782
|
+
if (data.before_clip_id != null && data.after_clip_id != null) {
|
|
1783
|
+
ctx.addIssue({
|
|
1784
|
+
code: z.ZodIssueCode.custom,
|
|
1785
|
+
message: "Cannot provide both before_clip_id and after_clip_id"
|
|
1786
|
+
});
|
|
1787
|
+
return;
|
|
1788
|
+
}
|
|
1789
|
+
const hasRelative = data.before_clip_id != null || data.after_clip_id != null;
|
|
1790
|
+
for (let i = 0; i < data.clips.length; i++) {
|
|
1791
|
+
const clip = data.clips[i];
|
|
1792
|
+
if (hasRelative && clip.start_ms != null) ctx.addIssue({
|
|
1793
|
+
code: z.ZodIssueCode.custom,
|
|
1794
|
+
message: `clips[${i}].start_ms must not be provided when using before_clip_id or after_clip_id`,
|
|
1795
|
+
path: [
|
|
1796
|
+
"clips",
|
|
1797
|
+
i,
|
|
1798
|
+
"start_ms"
|
|
1799
|
+
]
|
|
1800
|
+
});
|
|
1801
|
+
if (!hasRelative && clip.start_ms == null) ctx.addIssue({
|
|
1802
|
+
code: z.ZodIssueCode.custom,
|
|
1803
|
+
message: `clips[${i}].start_ms is required when not using relative positioning`,
|
|
1804
|
+
path: [
|
|
1805
|
+
"clips",
|
|
1806
|
+
i,
|
|
1807
|
+
"start_ms"
|
|
1808
|
+
]
|
|
1809
|
+
});
|
|
1810
|
+
const playIn = clip.play_in ?? 0;
|
|
1811
|
+
const playOut = clip.play_out ?? clip.media_duration_ms;
|
|
1812
|
+
if (playOut > clip.media_duration_ms) ctx.addIssue({
|
|
1813
|
+
code: z.ZodIssueCode.custom,
|
|
1814
|
+
message: `clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
|
|
1815
|
+
path: [
|
|
1816
|
+
"clips",
|
|
1817
|
+
i,
|
|
1818
|
+
"play_out"
|
|
1819
|
+
]
|
|
1820
|
+
});
|
|
1821
|
+
if (playIn >= playOut) ctx.addIssue({
|
|
1822
|
+
code: z.ZodIssueCode.custom,
|
|
1823
|
+
message: `clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
|
|
1824
|
+
path: [
|
|
1825
|
+
"clips",
|
|
1826
|
+
i,
|
|
1827
|
+
"play_in"
|
|
1828
|
+
]
|
|
1829
|
+
});
|
|
1830
|
+
}
|
|
1831
|
+
});
|
|
1832
|
+
//#endregion
|
|
1833
|
+
//#region src/editor/schemas/adjust-bgm-volume.ts
|
|
1834
|
+
const adjustBgmVolumeInputSchema = z.object({ bgm: z.array(z.object({
|
|
1835
|
+
bgm_id: clipIdSchema.describe("The bgm part ID to adjust volume for"),
|
|
1836
|
+
volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
|
|
1837
|
+
})).min(1).describe("List of bgm parts with their new volume settings") });
|
|
1838
|
+
//#endregion
|
|
1839
|
+
//#region src/editor/schemas/adjust-speech-volume.ts
|
|
1840
|
+
const adjustSpeechVolumeInputSchema = z.object({ speeches: z.array(z.object({
|
|
1841
|
+
speech_id: speechIdSchema.describe("The speech part ID to adjust volume for"),
|
|
1842
|
+
volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
|
|
1843
|
+
})).min(1).describe("List of speeches with their new volume settings") });
|
|
1844
|
+
//#endregion
|
|
1845
|
+
//#region src/editor/schemas/adjust-video-clip-duration.ts
|
|
1846
|
+
/**
|
|
1847
|
+
* Re-trim existing video clips (the user-facing "adjust duration" gesture is a
|
|
1848
|
+
* trim of the source window). The new `play_in` / `play_out` are the facts; the
|
|
1849
|
+
* effective timeline duration is derived from them and the clip's `speed_shift`,
|
|
1850
|
+
* and the change reflows downstream clips, speeches, and the timeline inside the
|
|
1851
|
+
* op's transaction (no caller-materialized cascade).
|
|
1852
|
+
*/
|
|
1853
|
+
const adjustVideoClipDurationInputSchema = z.object({ clips: z.array(z.object({
|
|
1854
|
+
clip_id: clipIdSchema.describe("The video clip part ID to re-trim"),
|
|
1855
|
+
play_in: timelineMsSchema.describe("New trim window start in the source media"),
|
|
1856
|
+
play_out: positiveMsSchema.describe("New trim window end in the source media")
|
|
1857
|
+
})).min(1).describe("Video clips with their new trim windows") });
|
|
1858
|
+
//#endregion
|
|
1859
|
+
//#region src/editor/schemas/adjust-video-clip-volume.ts
|
|
1860
|
+
const adjustVideoClipVolumeInputSchema = z.object({ clips: z.array(z.object({
|
|
1861
|
+
clip_id: clipIdSchema.describe("The video clip part ID to adjust volume for"),
|
|
1862
|
+
volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
|
|
1863
|
+
})).min(1).describe("List of video clips with their new volume settings") });
|
|
1864
|
+
//#endregion
|
|
1865
|
+
//#region src/editor/schemas/change-speech.ts
|
|
1866
|
+
/**
|
|
1867
|
+
* Change a speech's script or voice. Both re-run TTS upstream and return the
|
|
1868
|
+
* regenerated speech / caption parts in the same materialized shape as
|
|
1869
|
+
* `AddSpeeches` (`speech-assets.ts`); the op upserts them by id (the speech part
|
|
1870
|
+
* id is preserved across a re-TTS), re-seats at `start_ms`, and reflows. Old
|
|
1871
|
+
* caption parts no longer owned by the speech are removed via `caption_ids`.
|
|
1872
|
+
*/
|
|
1873
|
+
const changeSpeechScriptInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new script)");
|
|
1874
|
+
const changeSpeechVoiceInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new voice)");
|
|
1875
|
+
//#endregion
|
|
1876
|
+
//#region src/editor/schemas/delete-bgm.ts
|
|
1877
|
+
/**
|
|
1878
|
+
* Remove the document BGM. Pure document edit: clears the bgm lane and removes
|
|
1879
|
+
* the bgm part. Takes no input (a document holds at most one bgm); an empty
|
|
1880
|
+
* object keeps the op signature uniform with the rest.
|
|
1881
|
+
*/
|
|
1882
|
+
const deleteBgmInputSchema = z.object({}).describe("Remove the document BGM (no parameters)");
|
|
1883
|
+
//#endregion
|
|
1884
|
+
//#region src/editor/schemas/delete-speeches.ts
|
|
1885
|
+
/**
|
|
1886
|
+
* Delete speeches with their captions. Pure document edit (no side effect): the
|
|
1887
|
+
* op removes each speech part, cascade-deletes the captions it owns (via
|
|
1888
|
+
* `caption_ids` / `speech_part_id`), drops their track items, and reflows.
|
|
1889
|
+
*/
|
|
1890
|
+
const deleteSpeechesInputSchema = z.object({ speech_ids: speechIdsSchema.describe("Speech part IDs to delete (their captions cascade-delete)") });
|
|
1891
|
+
//#endregion
|
|
1892
|
+
//#region src/editor/schemas/delete-video-clips.ts
|
|
1893
|
+
/**
|
|
1894
|
+
* How a delete handles the anchored subtree (speeches anchored to a deleted clip,
|
|
1895
|
+
* and their captions) — a delete-op policy, not a data-model field (reference/17
|
|
1896
|
+
* §6). `cascade` (default) removes the subtree; `detach` keeps the direct
|
|
1897
|
+
* anchored children, re-pinning them to `absolute` so they stay on the timeline.
|
|
1898
|
+
*/
|
|
1899
|
+
const anchoredDeletePolicySchema = z.enum(["cascade", "detach"]);
|
|
1900
|
+
const deleteVideoClipsInputSchema = z.object({
|
|
1901
|
+
clip_ids: clipIdsSchema.describe("List of video clip part IDs to delete from the main track"),
|
|
1902
|
+
on_anchored: anchoredDeletePolicySchema.optional().describe("How to treat anchored children (default cascade)")
|
|
1903
|
+
});
|
|
1904
|
+
//#endregion
|
|
1905
|
+
//#region src/editor/schemas/move-speeches.ts
|
|
1906
|
+
/**
|
|
1907
|
+
* Move speeches in time. Pure document edit: the op re-seats each speech at its
|
|
1908
|
+
* new absolute `start_ms`; the cascade reassigns it to the host video clip,
|
|
1909
|
+
* resolves overlaps, and reflows. Captions follow their speech.
|
|
1910
|
+
*/
|
|
1911
|
+
const moveSpeechesInputSchema = z.object({ speeches: z.array(z.object({
|
|
1912
|
+
speech_id: speechIdSchema.describe("The speech part ID to move"),
|
|
1913
|
+
new_start_ms: timelineMsSchema.describe("New absolute start time on the timeline")
|
|
1914
|
+
})).min(1).describe("Speeches to move to new positions") });
|
|
1915
|
+
//#endregion
|
|
1916
|
+
//#region src/editor/schemas/move-video-clips.ts
|
|
1917
|
+
const moveVideoClipsInputSchema = z.object({ clips: z.array(z.object({
|
|
1918
|
+
clip_id: clipIdSchema.describe("The video clip part ID to move"),
|
|
1919
|
+
new_start_ms: timelineMsSchema.describe("New absolute start time in milliseconds on the timeline"),
|
|
1920
|
+
new_track_id: z.string().min(1).optional().describe("Target track ID to move the clip to (optional)")
|
|
1921
|
+
})).min(1).describe("List of video clips to move to new positions") });
|
|
1922
|
+
//#endregion
|
|
1923
|
+
//#region src/editor/schemas/replace-video-clip-content.ts
|
|
1924
|
+
/**
|
|
1925
|
+
* Replace the media backing existing video clips. The media import runs upstream
|
|
1926
|
+
* (Director); its stable result — the new media id, intrinsic length, and the
|
|
1927
|
+
* reset trim window — arrives materialized (see
|
|
1928
|
+
* `results/phase-4-side-effect-payload-contract.md` §4). Director resets
|
|
1929
|
+
* `play_in=0` / `play_out=media_duration_ms` and clears `speed_shift` on
|
|
1930
|
+
* replacement. The clip `part_id`s (hence their track items) are unchanged; the
|
|
1931
|
+
* editor reflows the main track from the new effective durations.
|
|
1932
|
+
*/
|
|
1933
|
+
const replaceVideoClipContentInputSchema = z.object({ clips: z.array(z.object({
|
|
1934
|
+
clip_id: clipIdSchema.describe("Existing video clip part ID to re-point"),
|
|
1935
|
+
origin_media_id: mediaIdSchema.describe("The new media asset ID"),
|
|
1936
|
+
media_duration_ms: positiveMsSchema.describe("The new media's intrinsic full length"),
|
|
1937
|
+
play_in: timelineMsSchema.describe("Trim window start in the new media (usually 0)"),
|
|
1938
|
+
play_out: positiveMsSchema.describe("Trim window end in the new media (usually = media_duration_ms)"),
|
|
1939
|
+
volume: volumeSchema
|
|
1940
|
+
})).min(1).describe("Video clips whose media is being replaced") });
|
|
1941
|
+
//#endregion
|
|
1942
|
+
//#region src/editor/schemas/set-bgm.ts
|
|
1943
|
+
/**
|
|
1944
|
+
* Set the document BGM. The media's stable result (storage key) arrives
|
|
1945
|
+
* materialized from upstream (see
|
|
1946
|
+
* `results/phase-4-side-effect-payload-contract.md` §3). The op upserts the bgm
|
|
1947
|
+
* part and seats it on the bgm lane; its effective length is always the whole
|
|
1948
|
+
* timeline, derived by the projection on read — so there is no `duration_ms`
|
|
1949
|
+
* input or fact (RFC 02 / `reference/16` §0b). A `bgm_id` lets the op replace an
|
|
1950
|
+
* existing bgm part by id.
|
|
1951
|
+
*/
|
|
1952
|
+
const setBgmInputSchema = z.object({
|
|
1953
|
+
bgm_id: z.string().min(1).describe("The bgm part ID to write"),
|
|
1954
|
+
audio_storage_key: z.string().min(1),
|
|
1955
|
+
origin_media_id: mediaIdSchema,
|
|
1956
|
+
volume: volumeSchema
|
|
1957
|
+
});
|
|
1958
|
+
//#endregion
|
|
1959
|
+
//#region src/editor/schemas/set-caption-style.ts
|
|
1960
|
+
/**
|
|
1961
|
+
* Set the caption visual style. GLOBAL by design: the style applies to every
|
|
1962
|
+
* caption part in the document — it carries NO `caption_id`. This mirrors the FE,
|
|
1963
|
+
* whose caption-style store (`caption-style.ts:persistCaptionStylePatch`) iterates
|
|
1964
|
+
* ALL captions and writes the same normalized style to each; the product has a
|
|
1965
|
+
* single document-wide caption style, not per-caption styling.
|
|
1966
|
+
*
|
|
1967
|
+
* Every field is optional and maps to a `CaptionStyle` attribute (snake_case
|
|
1968
|
+
* IDL). A field present in the input is written to every caption; a field ABSENT
|
|
1969
|
+
* from the input is left untouched on each caption (the editor merges the patch
|
|
1970
|
+
* onto each caption's existing style — this is a value edit, not a full-style
|
|
1971
|
+
* replace, so a partial patch such as "recolor only" does not wipe font size).
|
|
1972
|
+
*
|
|
1973
|
+
* Pure document edit, no cascade — captions keep their positions; only the style
|
|
1974
|
+
* sub-map of each caption part changes.
|
|
1975
|
+
*/
|
|
1976
|
+
const setCaptionStyleInputSchema = z.object({
|
|
1977
|
+
font_id: z.string().min(1).optional().describe("Font ID referencing a font from the font library"),
|
|
1978
|
+
font_size: z.number().positive().optional().describe("Font size in points"),
|
|
1979
|
+
font_color: z.string().min(1).optional().describe("Font color as hex string, e.g. \"#FFFFFF\""),
|
|
1980
|
+
font_weight: z.number().int().optional().describe("Numeric font weight, e.g. 400 or 700"),
|
|
1981
|
+
entrance_animation: z.string().optional().describe("Entrance animation preset ID, e.g. \"fade\" or \"none\""),
|
|
1982
|
+
entrance_animation_duration_ms: z.number().min(0).optional().describe("Entrance animation duration in ms"),
|
|
1983
|
+
stroke_color: z.string().min(1).optional().describe("Outline/stroke color as hex string, e.g. \"#000000\""),
|
|
1984
|
+
stroke_width: z.number().min(0).optional().describe("Outline/stroke width in pixels"),
|
|
1985
|
+
position_x: z.number().optional().describe("Caption center X as a fraction (0.0 to 1.0)"),
|
|
1986
|
+
position_y: z.number().optional().describe("Caption center Y as a fraction (0.0 to 1.0)")
|
|
1987
|
+
}).describe("Document-wide caption style patch (no caption_id; applies to every caption)");
|
|
1988
|
+
//#endregion
|
|
1989
|
+
//#region src/editor/schemas/set-caption-visibility.ts
|
|
1990
|
+
/**
|
|
1991
|
+
* Toggle caption visibility (the caption track's `is_hidden` flag). Pure
|
|
1992
|
+
* document edit, no cascade — captions keep their positions; only the lane's
|
|
1993
|
+
* hidden flag changes.
|
|
1994
|
+
*/
|
|
1995
|
+
const setCaptionVisibilityInputSchema = z.object({ is_hidden: z.boolean().describe("Whether the caption track is hidden") });
|
|
1996
|
+
//#endregion
|
|
1997
|
+
//#region src/editor/schemas/set-video-clip-speed-shift.ts
|
|
1998
|
+
/**
|
|
1999
|
+
* Set the playback speed of existing video clips. Per the speed-shift decision
|
|
2000
|
+
* (`reference/16` §0): the op writes only the `speed_shift` fact — it does NOT
|
|
2001
|
+
* store an effective `duration_ms` (projection derives it from the trim window /
|
|
2002
|
+
* speed) and does NOT scale anchored speeches' relative offsets (offsets stay
|
|
2003
|
+
* put; the cascade reflows absolute positions). A `null` speed_shift clears the
|
|
2004
|
+
* speed back to original (1×).
|
|
2005
|
+
*/
|
|
2006
|
+
const setVideoClipSpeedShiftInputSchema = z.object({ clips: z.array(z.object({
|
|
2007
|
+
clip_id: clipIdSchema.describe("The video clip part ID to set speed for"),
|
|
2008
|
+
speed_shift: speedShiftSchema.nullable().describe("The new speed setting, or null to reset to 1×")
|
|
2009
|
+
})).min(1).describe("Video clips with their new speed settings") });
|
|
2010
|
+
//#endregion
|
|
2011
|
+
//#region src/editor/schemas/index.ts
|
|
2012
|
+
var schemas_exports = /* @__PURE__ */ __exportAll({
|
|
2013
|
+
addSpeechesInputSchema: () => addSpeechesInputSchema,
|
|
2014
|
+
addVideoClipsInputSchema: () => addVideoClipsInputSchema,
|
|
2015
|
+
adjustBgmVolumeInputSchema: () => adjustBgmVolumeInputSchema,
|
|
2016
|
+
adjustSpeechVolumeInputSchema: () => adjustSpeechVolumeInputSchema,
|
|
2017
|
+
adjustVideoClipDurationInputSchema: () => adjustVideoClipDurationInputSchema,
|
|
2018
|
+
adjustVideoClipVolumeInputSchema: () => adjustVideoClipVolumeInputSchema,
|
|
2019
|
+
anchoredDeletePolicySchema: () => anchoredDeletePolicySchema,
|
|
2020
|
+
changeSpeechScriptInputSchema: () => changeSpeechScriptInputSchema,
|
|
2021
|
+
changeSpeechVoiceInputSchema: () => changeSpeechVoiceInputSchema,
|
|
2022
|
+
clipIdSchema: () => clipIdSchema,
|
|
2023
|
+
clipIdsSchema: () => clipIdsSchema,
|
|
2024
|
+
deleteBgmInputSchema: () => deleteBgmInputSchema,
|
|
2025
|
+
deleteSpeechesInputSchema: () => deleteSpeechesInputSchema,
|
|
2026
|
+
deleteVideoClipsInputSchema: () => deleteVideoClipsInputSchema,
|
|
2027
|
+
mediaIdSchema: () => mediaIdSchema,
|
|
2028
|
+
moveSpeechesInputSchema: () => moveSpeechesInputSchema,
|
|
2029
|
+
moveVideoClipsInputSchema: () => moveVideoClipsInputSchema,
|
|
2030
|
+
positiveMsSchema: () => positiveMsSchema,
|
|
2031
|
+
replaceVideoClipContentInputSchema: () => replaceVideoClipContentInputSchema,
|
|
2032
|
+
setBgmInputSchema: () => setBgmInputSchema,
|
|
2033
|
+
setCaptionStyleInputSchema: () => setCaptionStyleInputSchema,
|
|
2034
|
+
setCaptionVisibilityInputSchema: () => setCaptionVisibilityInputSchema,
|
|
2035
|
+
setVideoClipSpeedShiftInputSchema: () => setVideoClipSpeedShiftInputSchema,
|
|
2036
|
+
speechAssetsSchema: () => speechAssetsSchema,
|
|
2037
|
+
speechIdSchema: () => speechIdSchema,
|
|
2038
|
+
speechIdsSchema: () => speechIdsSchema,
|
|
2039
|
+
speedShiftSchema: () => speedShiftSchema,
|
|
2040
|
+
timelineMsSchema: () => timelineMsSchema,
|
|
2041
|
+
voiceSchema: () => voiceSchema,
|
|
2042
|
+
volumeSchema: () => volumeSchema
|
|
2043
|
+
});
|
|
2044
|
+
//#endregion
|
|
2045
|
+
//#region src/editor/snapshot-utils.ts
|
|
2046
|
+
function isMap(value) {
|
|
2047
|
+
return value instanceof Map;
|
|
2048
|
+
}
|
|
2049
|
+
function snapshotToPlain(value) {
|
|
2050
|
+
if (value instanceof Map) {
|
|
2051
|
+
const obj = {};
|
|
2052
|
+
for (const [k, v] of value) obj[String(k)] = snapshotToPlain(v);
|
|
2053
|
+
return obj;
|
|
2054
|
+
}
|
|
2055
|
+
if (Array.isArray(value)) return value.map(snapshotToPlain);
|
|
2056
|
+
return value;
|
|
2057
|
+
}
|
|
2058
|
+
function getAt(snapshot, ...keys) {
|
|
2059
|
+
let cur = snapshot;
|
|
2060
|
+
for (const k of keys) if (cur instanceof Map) cur = cur.get(k);
|
|
2061
|
+
else if (cur != null && typeof cur === "object") cur = cur[k];
|
|
2062
|
+
else return;
|
|
2063
|
+
return cur;
|
|
2064
|
+
}
|
|
2065
|
+
function readMainTrackItems(snapshot) {
|
|
2066
|
+
const tracks = getAt(snapshot, "tracks");
|
|
2067
|
+
const items = getAt(Array.isArray(tracks) ? tracks.find((t) => getAt(t, "parts_kind") === "video_clip") : void 0, "items");
|
|
2068
|
+
if (!Array.isArray(items)) return [];
|
|
2069
|
+
return items.map((item) => {
|
|
2070
|
+
if (item instanceof Map) return { part_id: optionalString(item.get("part_id")) };
|
|
2071
|
+
if (item != null && typeof item === "object") return { part_id: optionalString(item.part_id) };
|
|
2072
|
+
return { part_id: void 0 };
|
|
2073
|
+
});
|
|
2074
|
+
}
|
|
2075
|
+
/**
|
|
2076
|
+
* The effective timeline duration of a part read from a raw snapshot. No part
|
|
2077
|
+
* stores a derived `duration_ms` in authoritative state (reference/17 §5): a
|
|
2078
|
+
* video clip's length is its trim window `play_out - play_in` over the speed
|
|
2079
|
+
* multiplier; a speech's is its intrinsic `media_duration_ms`; a caption's is its
|
|
2080
|
+
* `initial_duration_ms`. Falls back to `fallback` when no source value is
|
|
2081
|
+
* resolvable (e.g. an unknown/raw part).
|
|
2082
|
+
*/
|
|
2083
|
+
function readPartDurationMs(snapshot, partId, fallback = 1e3) {
|
|
2084
|
+
const part = readPart(snapshot, partId);
|
|
2085
|
+
if (part == null) return fallback;
|
|
2086
|
+
let sourceMs;
|
|
2087
|
+
if (part.part_kind === "video_clip") {
|
|
2088
|
+
const playIn = numericValue(part.play_in);
|
|
2089
|
+
const playOut = numericValue(part.play_out);
|
|
2090
|
+
if (playIn != null && playOut != null) {
|
|
2091
|
+
const speed = videoClipSpeed(part.speed_shift);
|
|
2092
|
+
sourceMs = Math.round((playOut - playIn) / speed);
|
|
2093
|
+
}
|
|
2094
|
+
} else if (part.part_kind === "speech") sourceMs = numericValue(part.media_duration_ms);
|
|
2095
|
+
else if (part.part_kind === "caption") sourceMs = numericValue(part.initial_duration_ms);
|
|
2096
|
+
return sourceMs != null && Number.isFinite(sourceMs) ? sourceMs : fallback;
|
|
2097
|
+
}
|
|
2098
|
+
function numericValue(value) {
|
|
2099
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
2100
|
+
}
|
|
2101
|
+
/** The linear speed multiplier of a raw `speed_shift` blob, defaulting to 1. */
|
|
2102
|
+
function videoClipSpeed(speedShift) {
|
|
2103
|
+
const speed = speedShift?.config?.linear?.speed;
|
|
2104
|
+
return typeof speed === "number" && Number.isFinite(speed) && speed > 0 ? speed : 1;
|
|
2105
|
+
}
|
|
2106
|
+
function readPart(snapshot, partId) {
|
|
2107
|
+
const part = getAt(snapshot, "part_library", partId);
|
|
2108
|
+
if (part == null) return null;
|
|
2109
|
+
const plain = snapshotToPlain(part);
|
|
2110
|
+
if ("video_clip" in plain && plain.video_clip != null) return {
|
|
2111
|
+
...plain.video_clip,
|
|
2112
|
+
part_kind: "video_clip"
|
|
2113
|
+
};
|
|
2114
|
+
if ("speech" in plain && plain.speech != null) return {
|
|
2115
|
+
...plain.speech,
|
|
2116
|
+
part_kind: "speech"
|
|
2117
|
+
};
|
|
2118
|
+
if ("caption" in plain && plain.caption != null) return {
|
|
2119
|
+
...plain.caption,
|
|
2120
|
+
part_kind: "caption"
|
|
2121
|
+
};
|
|
2122
|
+
if ("bgm" in plain && plain.bgm != null) return {
|
|
2123
|
+
...plain.bgm,
|
|
2124
|
+
part_kind: "bgm"
|
|
2125
|
+
};
|
|
2126
|
+
return plain;
|
|
2127
|
+
}
|
|
2128
|
+
function optionalString(value) {
|
|
2129
|
+
return typeof value === "string" ? value : void 0;
|
|
2130
|
+
}
|
|
2131
|
+
//#endregion
|
|
2132
|
+
//#region src/editor/schema-validator.ts
|
|
2133
|
+
var ValidationError = class extends Error {
|
|
2134
|
+
code;
|
|
2135
|
+
context;
|
|
2136
|
+
constructor(code, message, context) {
|
|
2137
|
+
super(`[${code}] ${message}`);
|
|
2138
|
+
this.name = "ValidationError";
|
|
2139
|
+
this.code = code;
|
|
2140
|
+
this.context = context;
|
|
2141
|
+
}
|
|
2142
|
+
};
|
|
2143
|
+
var SchemaValidator = class {
|
|
2144
|
+
validateMoveVideoClips(input, doc) {
|
|
2145
|
+
const parsed = this.parse(moveVideoClipsInputSchema, input, "move_invalid_input");
|
|
2146
|
+
const knownIds = this.mainTrackIds(doc);
|
|
2147
|
+
for (const clip of parsed.clips) if (!knownIds.has(clip.clip_id)) throw new ValidationError("move_clip_not_found", `clip_id "${clip.clip_id}" not present on main_track`, { clip_id: clip.clip_id });
|
|
2148
|
+
}
|
|
2149
|
+
validateDeleteVideoClips(input, doc) {
|
|
2150
|
+
const parsed = this.parse(deleteVideoClipsInputSchema, input, "delete_invalid_input");
|
|
2151
|
+
const knownIds = this.mainTrackIds(doc);
|
|
2152
|
+
for (const id of parsed.clip_ids) if (!knownIds.has(id)) throw new ValidationError("delete_clip_not_found", `clip_id "${id}" not present on main_track`, { clip_id: id });
|
|
2153
|
+
}
|
|
2154
|
+
validateAddVideoClips(input, doc) {
|
|
2155
|
+
const parsed = this.parse(addVideoClipsInputSchema, input, "add_invalid_input");
|
|
2156
|
+
const knownIds = this.mainTrackIds(doc);
|
|
2157
|
+
if (parsed.before_clip_id != null && !knownIds.has(parsed.before_clip_id)) throw new ValidationError("add_before_clip_not_found", `before_clip_id "${parsed.before_clip_id}" not on main_track`, { before_clip_id: parsed.before_clip_id });
|
|
2158
|
+
if (parsed.after_clip_id != null && !knownIds.has(parsed.after_clip_id)) throw new ValidationError("add_after_clip_not_found", `after_clip_id "${parsed.after_clip_id}" not on main_track`, { after_clip_id: parsed.after_clip_id });
|
|
2159
|
+
}
|
|
2160
|
+
validateAdjustVideoClipVolume(input, doc) {
|
|
2161
|
+
const parsed = this.parse(adjustVideoClipVolumeInputSchema, input, "adjust_volume_invalid_input");
|
|
2162
|
+
const snapshot = doc.snapshot();
|
|
2163
|
+
for (const c of parsed.clips) this.assertPartKind(snapshot, c.clip_id, "video_clip", "adjust_volume");
|
|
2164
|
+
}
|
|
2165
|
+
validateSetVideoClipSpeedShift(input, doc) {
|
|
2166
|
+
const parsed = this.parse(setVideoClipSpeedShiftInputSchema, input, "set_speed_invalid_input");
|
|
2167
|
+
const snapshot = doc.snapshot();
|
|
2168
|
+
for (const c of parsed.clips) this.assertPartKind(snapshot, c.clip_id, "video_clip", "set_speed");
|
|
2169
|
+
}
|
|
2170
|
+
validateReplaceVideoClipContent(input, doc) {
|
|
2171
|
+
const parsed = this.parse(replaceVideoClipContentInputSchema, input, "replace_content_invalid_input");
|
|
2172
|
+
const snapshot = doc.snapshot();
|
|
2173
|
+
for (const c of parsed.clips) {
|
|
2174
|
+
this.assertPartKind(snapshot, c.clip_id, "video_clip", "replace_content");
|
|
2175
|
+
if (c.play_in >= c.play_out || c.play_out > c.media_duration_ms) throw new ValidationError("replace_content_invalid_window", `trim window [${c.play_in}, ${c.play_out}] must be non-empty and within media_duration_ms ${c.media_duration_ms}`, {
|
|
2176
|
+
clip_id: c.clip_id,
|
|
2177
|
+
play_in: c.play_in,
|
|
2178
|
+
play_out: c.play_out,
|
|
2179
|
+
media_duration_ms: c.media_duration_ms
|
|
2180
|
+
});
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
validateAdjustVideoClipDuration(input, doc) {
|
|
2184
|
+
const parsed = this.parse(adjustVideoClipDurationInputSchema, input, "adjust_duration_invalid_input");
|
|
2185
|
+
const snapshot = doc.snapshot();
|
|
2186
|
+
for (const c of parsed.clips) {
|
|
2187
|
+
this.assertPartKind(snapshot, c.clip_id, "video_clip", "adjust_duration");
|
|
2188
|
+
if (c.play_out <= c.play_in) throw new ValidationError("adjust_duration_invalid_window", `play_out must be greater than play_in`, {
|
|
2189
|
+
clip_id: c.clip_id,
|
|
2190
|
+
play_in: c.play_in,
|
|
2191
|
+
play_out: c.play_out
|
|
2192
|
+
});
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
validateAdjustSpeechVolume(input, doc) {
|
|
2196
|
+
const parsed = this.parse(adjustSpeechVolumeInputSchema, input, "adjust_speech_volume_invalid_input");
|
|
2197
|
+
const snapshot = doc.snapshot();
|
|
2198
|
+
for (const s of parsed.speeches) this.assertPartKind(snapshot, s.speech_id, "speech", "adjust_speech_volume");
|
|
2199
|
+
}
|
|
2200
|
+
validateAdjustBgmVolume(input, doc) {
|
|
2201
|
+
const parsed = this.parse(adjustBgmVolumeInputSchema, input, "adjust_bgm_volume_invalid_input");
|
|
2202
|
+
const snapshot = doc.snapshot();
|
|
2203
|
+
for (const b of parsed.bgm) this.assertPartKind(snapshot, b.bgm_id, "bgm", "adjust_bgm_volume");
|
|
2204
|
+
}
|
|
2205
|
+
validateAddSpeeches(input, doc) {
|
|
2206
|
+
const parsed = this.parse(addSpeechesInputSchema, input, "add_speeches_invalid_input");
|
|
2207
|
+
this.assertCaptionsOwned(parsed, "add_speeches");
|
|
2208
|
+
this.assertAnchorsExist(parsed, doc, "add_speeches");
|
|
2209
|
+
}
|
|
2210
|
+
validateChangeSpeechScript(input, doc) {
|
|
2211
|
+
const parsed = this.parse(changeSpeechScriptInputSchema, input, "change_script_invalid_input");
|
|
2212
|
+
this.assertCaptionsOwned(parsed, "change_script");
|
|
2213
|
+
this.assertSpeechesExist(parsed, doc, "change_script");
|
|
2214
|
+
this.assertAnchorsExist(parsed, doc, "change_script");
|
|
2215
|
+
}
|
|
2216
|
+
validateChangeSpeechVoice(input, doc) {
|
|
2217
|
+
const parsed = this.parse(changeSpeechVoiceInputSchema, input, "change_voice_invalid_input");
|
|
2218
|
+
this.assertCaptionsOwned(parsed, "change_voice");
|
|
2219
|
+
this.assertSpeechesExist(parsed, doc, "change_voice");
|
|
2220
|
+
this.assertAnchorsExist(parsed, doc, "change_voice");
|
|
2221
|
+
}
|
|
2222
|
+
validateDeleteSpeeches(input, doc) {
|
|
2223
|
+
const parsed = this.parse(deleteSpeechesInputSchema, input, "delete_speeches_invalid_input");
|
|
2224
|
+
const snapshot = doc.snapshot();
|
|
2225
|
+
for (const id of parsed.speech_ids) this.assertPartKind(snapshot, id, "speech", "delete_speeches");
|
|
2226
|
+
}
|
|
2227
|
+
validateMoveSpeeches(input, doc) {
|
|
2228
|
+
const parsed = this.parse(moveSpeechesInputSchema, input, "move_speeches_invalid_input");
|
|
2229
|
+
const snapshot = doc.snapshot();
|
|
2230
|
+
for (const s of parsed.speeches) this.assertPartKind(snapshot, s.speech_id, "speech", "move_speeches");
|
|
2231
|
+
}
|
|
2232
|
+
validateSetBgm(input, _doc) {
|
|
2233
|
+
this.parse(setBgmInputSchema, input, "set_bgm_invalid_input");
|
|
2234
|
+
}
|
|
2235
|
+
validateDeleteBgm(input, _doc) {
|
|
2236
|
+
this.parse(deleteBgmInputSchema, input, "delete_bgm_invalid_input");
|
|
2237
|
+
}
|
|
2238
|
+
validateSetCaptionVisibility(input, _doc) {
|
|
2239
|
+
this.parse(setCaptionVisibilityInputSchema, input, "set_caption_visibility_invalid_input");
|
|
2240
|
+
}
|
|
2241
|
+
validateSetCaptionStyle(input, _doc) {
|
|
2242
|
+
this.parse(setCaptionStyleInputSchema, input, "set_caption_style_invalid_input");
|
|
2243
|
+
}
|
|
2244
|
+
parse(schema, input, code) {
|
|
2245
|
+
const parsed = schema.safeParse(input);
|
|
2246
|
+
if (!parsed.success) throw new ValidationError(code, parsed.error.message, { issues: parsed.error.issues });
|
|
2247
|
+
return parsed.data;
|
|
2248
|
+
}
|
|
2249
|
+
mainTrackIds(doc) {
|
|
2250
|
+
const items = readMainTrackItems(doc.snapshot());
|
|
2251
|
+
return new Set(items.map((it) => it.part_id).filter((id) => id != null));
|
|
2252
|
+
}
|
|
2253
|
+
/** Every caption a speech declares in `caption_ids` must be supplied in `captions`. */
|
|
2254
|
+
assertCaptionsOwned(assets, opName) {
|
|
2255
|
+
const supplied = new Set(assets.captions.map((c) => c.caption_id));
|
|
2256
|
+
for (const speech of assets.speeches) for (const captionId of speech.caption_ids) if (!supplied.has(captionId)) throw new ValidationError(`${opName}_caption_not_supplied`, `speech declares caption not in payload`, {
|
|
2257
|
+
speech_id: speech.speech_id,
|
|
2258
|
+
caption_id: captionId
|
|
2259
|
+
});
|
|
2260
|
+
}
|
|
2261
|
+
/** Each regenerated speech (re-TTS) must already exist in the document. */
|
|
2262
|
+
assertSpeechesExist(assets, doc, opName) {
|
|
2263
|
+
const snapshot = doc.snapshot();
|
|
2264
|
+
for (const speech of assets.speeches) this.assertPartKind(snapshot, speech.speech_id, "speech", opName);
|
|
2265
|
+
}
|
|
2266
|
+
/**
|
|
2267
|
+
* Each speech's `anchor_part_id` must point at a video clip that already
|
|
2268
|
+
* exists. A relative speech anchored to a missing clip cannot be positioned
|
|
2269
|
+
* by the projection and has no host to recover to (RFC 02 §4/§11.1), so the
|
|
2270
|
+
* write must be rejected rather than landing a dangling reference. (Restores
|
|
2271
|
+
* the dangling-reference guard the old `validateMaterializedPatch` carried;
|
|
2272
|
+
* ADR 0009.)
|
|
2273
|
+
*/
|
|
2274
|
+
assertAnchorsExist(assets, doc, opName) {
|
|
2275
|
+
const snapshot = doc.snapshot();
|
|
2276
|
+
for (const speech of assets.speeches) this.assertPartKind(snapshot, speech.anchor_part_id, "video_clip", `${opName}_anchor`);
|
|
2277
|
+
}
|
|
2278
|
+
assertPartKind(snapshot, partId, kind, opName) {
|
|
2279
|
+
const part = readPart(snapshot, partId);
|
|
2280
|
+
if (part == null) throw new ValidationError(`${opName}_part_not_found`, `part_library has no entry for "${partId}"`, { part_id: partId });
|
|
2281
|
+
if (part.part_kind !== kind) throw new ValidationError(`${opName}_wrong_part_kind`, `part_id "${partId}" is kind "${String(part.part_kind)}", expected "${kind}"`, {
|
|
2282
|
+
part_id: partId,
|
|
2283
|
+
part_kind: part.part_kind
|
|
2284
|
+
});
|
|
2285
|
+
}
|
|
2286
|
+
};
|
|
2287
|
+
//#endregion
|
|
2288
|
+
//#region src/timeline-core/locate.ts
|
|
2289
|
+
/**
|
|
2290
|
+
* The flow-ordered main-track clip ranges (cumulative effective durations from
|
|
2291
|
+
* 0). Empty-media gap fillers are not in authoritative state, so this reflects
|
|
2292
|
+
* only the real clips the draft stores.
|
|
2293
|
+
*/
|
|
2294
|
+
function mainTrackRanges(draft) {
|
|
2295
|
+
const partLibrary = readPartLibrary(draft);
|
|
2296
|
+
const items = (draft.tracks ?? []).find((t) => t?.parts_kind === "video_clip")?.items ?? [];
|
|
2297
|
+
const ranges = [];
|
|
2298
|
+
let cursor = 0;
|
|
2299
|
+
for (const item of items) {
|
|
2300
|
+
const partId = item?.part_id;
|
|
2301
|
+
if (partId == null) continue;
|
|
2302
|
+
const clip = partLibrary[partId]?.video_clip;
|
|
2303
|
+
if (clip == null) continue;
|
|
2304
|
+
const durationMs = effectiveVideoClipDurationMs(clip);
|
|
2305
|
+
ranges.push({
|
|
2306
|
+
partId,
|
|
2307
|
+
startMs: cursor,
|
|
2308
|
+
endMs: cursor + durationMs
|
|
2309
|
+
});
|
|
2310
|
+
cursor += durationMs;
|
|
2311
|
+
}
|
|
2312
|
+
return ranges;
|
|
2313
|
+
}
|
|
2314
|
+
/**
|
|
2315
|
+
* Pick the host video clip an absolute time lands in, with the harness two-sided
|
|
2316
|
+
* fallback: before the first clip → first clip; after the last → last clip.
|
|
2317
|
+
* Returns null only when there is no clip at all (caller leaves the item as-is).
|
|
2318
|
+
*/
|
|
2319
|
+
function hostForAbsMs(ranges, absMs) {
|
|
2320
|
+
if (ranges.length === 0) return null;
|
|
2321
|
+
const hit = ranges.find((r) => r.startMs <= absMs && absMs < r.endMs);
|
|
2322
|
+
if (hit != null) return hit;
|
|
2323
|
+
return absMs < ranges[0].startMs ? ranges[0] : ranges[ranges.length - 1];
|
|
2324
|
+
}
|
|
2325
|
+
/**
|
|
2326
|
+
* Build an `anchored` time position anchoring `absMs` to the host clip it lands
|
|
2327
|
+
* in (offset clamped to a non-negative integer). Falls back to `absolute` when
|
|
2328
|
+
* there is no host clip. Pair with `fallbackAbsMs = absMs` on the item.
|
|
2329
|
+
*/
|
|
2330
|
+
function relativePositionForAbs(ranges, absMs) {
|
|
2331
|
+
const host = hostForAbsMs(ranges, absMs);
|
|
2332
|
+
if (host == null) return {
|
|
2333
|
+
mode: "absolute",
|
|
2334
|
+
offsetMs: Math.round(absMs)
|
|
2335
|
+
};
|
|
2336
|
+
return {
|
|
2337
|
+
mode: "anchored",
|
|
2338
|
+
anchorPartId: host.partId,
|
|
2339
|
+
offsetMs: Math.max(0, Math.round(absMs - host.startMs))
|
|
2340
|
+
};
|
|
2341
|
+
}
|
|
2342
|
+
function readPartLibrary(draft) {
|
|
2343
|
+
const out = {};
|
|
2344
|
+
for (const [partId, part] of recordEntries(draft.part_library)) if (part != null) out[partId] = part;
|
|
2345
|
+
return out;
|
|
2346
|
+
}
|
|
2347
|
+
//#endregion
|
|
2348
|
+
//#region src/editor/semantic-editor.ts
|
|
2349
|
+
var SemanticEditor = class {
|
|
2350
|
+
doc;
|
|
2351
|
+
validator;
|
|
2352
|
+
constructor(doc, validator = new SchemaValidator()) {
|
|
2353
|
+
this.doc = doc;
|
|
2354
|
+
this.validator = validator;
|
|
2355
|
+
}
|
|
2356
|
+
async moveVideoClips(input, options) {
|
|
2357
|
+
this.validator.validateMoveVideoClips(input, this.doc);
|
|
2358
|
+
this.doc.transact((draft) => {
|
|
2359
|
+
const items = mainTrackItems(draft);
|
|
2360
|
+
if (items == null) return;
|
|
2361
|
+
const beforeRanges = mainTrackRanges(draft);
|
|
2362
|
+
for (const clip of input.clips) {
|
|
2363
|
+
const layout = this.computeMainTrackLayout(draft);
|
|
2364
|
+
const fromItem = layout.find((it) => it.part_id === clip.clip_id);
|
|
2365
|
+
if (fromItem == null) throw new Error(`moveVideoClips: clip "${clip.clip_id}" disappeared mid-batch`);
|
|
2366
|
+
const toIndex = this.indexForStartMs(layout, clip.new_start_ms, fromItem.index);
|
|
2367
|
+
if (toIndex === fromItem.index) continue;
|
|
2368
|
+
moveItem(items, fromItem.index, toIndex);
|
|
2369
|
+
}
|
|
2370
|
+
reparentSpeechesAfterMainTrackChange(draft, beforeRanges);
|
|
2371
|
+
}, audit("MoveVideoClips", input, options));
|
|
2372
|
+
}
|
|
2373
|
+
async deleteVideoClips(input, options) {
|
|
2374
|
+
this.validator.validateDeleteVideoClips(input, this.doc);
|
|
2375
|
+
const targets = new Set(input.clip_ids);
|
|
2376
|
+
const policy = input.on_anchored ?? "cascade";
|
|
2377
|
+
this.doc.transact((draft) => {
|
|
2378
|
+
const track = mainTrackRow(draft);
|
|
2379
|
+
if (track?.items == null) return;
|
|
2380
|
+
if (policy === "detach") for (const clipId of targets) detachAnchoredChildren(draft, clipId);
|
|
2381
|
+
track.items = track.items.filter((item) => item.part_id == null || !targets.has(item.part_id));
|
|
2382
|
+
for (const clipId of targets) {
|
|
2383
|
+
if (policy === "cascade") deleteAnchoredSubtree(draft, clipId);
|
|
2384
|
+
deletePart(draft, clipId);
|
|
2385
|
+
}
|
|
2386
|
+
}, audit("DeleteVideoClips", input, options));
|
|
2387
|
+
}
|
|
2388
|
+
async addVideoClips(input, options) {
|
|
2389
|
+
this.validator.validateAddVideoClips(input, this.doc);
|
|
2390
|
+
const insertIndex = this.computeAddInsertIndex(input);
|
|
2391
|
+
this.doc.transact((draft) => {
|
|
2392
|
+
const track = ensureMainTrack(draft);
|
|
2393
|
+
track.items ??= [];
|
|
2394
|
+
let at = insertIndex;
|
|
2395
|
+
for (const clip of input.clips) {
|
|
2396
|
+
const partId = generatePartId("clip");
|
|
2397
|
+
setPart(draft, partId, { video_clip: {
|
|
2398
|
+
id: partId,
|
|
2399
|
+
kind: "video_clip",
|
|
2400
|
+
play_in: clip.play_in ?? 0,
|
|
2401
|
+
play_out: clip.play_out ?? clip.media_duration_ms,
|
|
2402
|
+
volume: 0,
|
|
2403
|
+
origin_media_id: clip.media_id
|
|
2404
|
+
} });
|
|
2405
|
+
track.items.splice(at, 0, {
|
|
2406
|
+
part_id: partId,
|
|
2407
|
+
time_position: { mode: "sequential" },
|
|
2408
|
+
fallback_abs_ms: void 0
|
|
2409
|
+
});
|
|
2410
|
+
at += 1;
|
|
2411
|
+
}
|
|
2412
|
+
}, audit("AddVideoClips", input, options));
|
|
2413
|
+
}
|
|
2414
|
+
async adjustVideoClipVolume(input, options) {
|
|
2415
|
+
this.validator.validateAdjustVideoClipVolume(input, this.doc);
|
|
2416
|
+
this.doc.transact((draft) => {
|
|
2417
|
+
for (const c of input.clips) {
|
|
2418
|
+
const videoClip = draft.part_library?.[c.clip_id]?.video_clip;
|
|
2419
|
+
if (videoClip == null) continue;
|
|
2420
|
+
setPart(draft, c.clip_id, { video_clip: {
|
|
2421
|
+
...videoClip,
|
|
2422
|
+
kind: "video_clip",
|
|
2423
|
+
volume: c.volume
|
|
2424
|
+
} });
|
|
2425
|
+
}
|
|
2426
|
+
}, audit("AdjustVideoClipVolume", input, options));
|
|
2427
|
+
}
|
|
2428
|
+
async setVideoClipSpeedShift(input, options) {
|
|
2429
|
+
this.validator.validateSetVideoClipSpeedShift(input, this.doc);
|
|
2430
|
+
this.doc.transact((draft) => {
|
|
2431
|
+
for (const clip of input.clips) {
|
|
2432
|
+
const videoClip = draft.part_library?.[clip.clip_id]?.video_clip;
|
|
2433
|
+
if (videoClip == null) continue;
|
|
2434
|
+
const next = {
|
|
2435
|
+
...videoClip,
|
|
2436
|
+
kind: "video_clip",
|
|
2437
|
+
speed_shift: normalizeSpeedShift(clip.speed_shift)
|
|
2438
|
+
};
|
|
2439
|
+
setPart(draft, clip.clip_id, { video_clip: next });
|
|
2440
|
+
}
|
|
2441
|
+
}, audit("SetVideoClipSpeedShift", input, options));
|
|
2442
|
+
}
|
|
2443
|
+
async adjustSpeechVolume(input, options) {
|
|
2444
|
+
this.validator.validateAdjustSpeechVolume(input, this.doc);
|
|
2445
|
+
this.doc.transact((draft) => {
|
|
2446
|
+
for (const s of input.speeches) {
|
|
2447
|
+
const speech = draft.part_library?.[s.speech_id]?.speech;
|
|
2448
|
+
if (speech == null) continue;
|
|
2449
|
+
setPart(draft, s.speech_id, { speech: {
|
|
2450
|
+
...speech,
|
|
2451
|
+
kind: "speech",
|
|
2452
|
+
volume: s.volume
|
|
2453
|
+
} });
|
|
2454
|
+
}
|
|
2455
|
+
}, audit("AdjustSpeechVolume", input, options));
|
|
2456
|
+
}
|
|
2457
|
+
async adjustBgmVolume(input, options) {
|
|
2458
|
+
this.validator.validateAdjustBgmVolume(input, this.doc);
|
|
2459
|
+
this.doc.transact((draft) => {
|
|
2460
|
+
for (const b of input.bgm) {
|
|
2461
|
+
const bgm = draft.part_library?.[b.bgm_id]?.bgm;
|
|
2462
|
+
if (bgm == null) continue;
|
|
2463
|
+
setPart(draft, b.bgm_id, { bgm: {
|
|
2464
|
+
...bgm,
|
|
2465
|
+
kind: "bgm",
|
|
2466
|
+
volume: b.volume
|
|
2467
|
+
} });
|
|
2468
|
+
}
|
|
2469
|
+
}, audit("AdjustBgmVolume", input, options));
|
|
2470
|
+
}
|
|
2471
|
+
/**
|
|
2472
|
+
* Replace the media backing existing video clips. The new media's stable
|
|
2473
|
+
* result (new media id, intrinsic length, reset trim window) is materialized
|
|
2474
|
+
* upstream; the clip ids and their track items are unchanged. Only the part
|
|
2475
|
+
* facts change — effective duration and downstream positions are derived by
|
|
2476
|
+
* the projection on read.
|
|
2477
|
+
*/
|
|
2478
|
+
async replaceVideoClipContent(input, options) {
|
|
2479
|
+
this.validator.validateReplaceVideoClipContent(input, this.doc);
|
|
2480
|
+
this.doc.transact((draft) => {
|
|
2481
|
+
for (const clip of input.clips) {
|
|
2482
|
+
const videoClip = draft.part_library?.[clip.clip_id]?.video_clip;
|
|
2483
|
+
if (videoClip == null) continue;
|
|
2484
|
+
setPart(draft, clip.clip_id, { video_clip: {
|
|
2485
|
+
...videoClip,
|
|
2486
|
+
kind: "video_clip",
|
|
2487
|
+
origin_media_id: clip.origin_media_id,
|
|
2488
|
+
play_in: clip.play_in,
|
|
2489
|
+
play_out: clip.play_out,
|
|
2490
|
+
volume: clip.volume,
|
|
2491
|
+
speed_shift: void 0
|
|
2492
|
+
} });
|
|
2493
|
+
}
|
|
2494
|
+
}, audit("ReplaceVideoClipContent", input, options));
|
|
2495
|
+
}
|
|
2496
|
+
/**
|
|
2497
|
+
* Re-trim clips. Only the `play_in` / `play_out` facts change; the new
|
|
2498
|
+
* effective duration and the resulting downstream reflow are derived by the
|
|
2499
|
+
* projection on read (anchored speeches follow their host clip automatically).
|
|
2500
|
+
*/
|
|
2501
|
+
async adjustVideoClipDuration(input, options) {
|
|
2502
|
+
this.validator.validateAdjustVideoClipDuration(input, this.doc);
|
|
2503
|
+
this.doc.transact((draft) => {
|
|
2504
|
+
for (const clip of input.clips) {
|
|
2505
|
+
const videoClip = draft.part_library?.[clip.clip_id]?.video_clip;
|
|
2506
|
+
if (videoClip == null) continue;
|
|
2507
|
+
setPart(draft, clip.clip_id, { video_clip: {
|
|
2508
|
+
...videoClip,
|
|
2509
|
+
kind: "video_clip",
|
|
2510
|
+
play_in: clip.play_in,
|
|
2511
|
+
play_out: clip.play_out
|
|
2512
|
+
} });
|
|
2513
|
+
}
|
|
2514
|
+
refreshAnchoredFallbackAbs(draft);
|
|
2515
|
+
}, audit("AdjustVideoClipDuration", input, options));
|
|
2516
|
+
}
|
|
2517
|
+
/**
|
|
2518
|
+
* Add speeches (and their captions). TTS runs upstream; the materialized
|
|
2519
|
+
* speech / caption parts arrive in `input`, each carrying its host clip
|
|
2520
|
+
* `anchor_part_id` + `offset_ms`. The editor writes the parts and their
|
|
2521
|
+
* `anchored` `time_position` facts verbatim — no write-time host-picking, no
|
|
2522
|
+
* cascade. The projection derives absolute positions on read.
|
|
2523
|
+
*/
|
|
2524
|
+
async addSpeeches(input, options) {
|
|
2525
|
+
this.validator.validateAddSpeeches(input, this.doc);
|
|
2526
|
+
this.doc.transact((draft) => {
|
|
2527
|
+
writeSpeechAssets(draft, input);
|
|
2528
|
+
}, audit("AddSpeeches", input, options));
|
|
2529
|
+
}
|
|
2530
|
+
/** Delete speeches with their captions; the subtree is removed (§9.2). Surviving lanes keep their facts. */
|
|
2531
|
+
async deleteSpeeches(input, options) {
|
|
2532
|
+
this.validator.validateDeleteSpeeches(input, this.doc);
|
|
2533
|
+
this.doc.transact((draft) => {
|
|
2534
|
+
for (const speechId of input.speech_ids) deleteSpeechSubtree(draft, speechId);
|
|
2535
|
+
}, audit("DeleteSpeeches", input, options));
|
|
2536
|
+
}
|
|
2537
|
+
/**
|
|
2538
|
+
* Move speeches in time (§9.1). One forward positioning pass: anchor each
|
|
2539
|
+
* speech to the main-track clip its new absolute start lands in and write the
|
|
2540
|
+
* resulting `anchored` `time_position` fact. No cascade — the projection
|
|
2541
|
+
* derives absolute positions on read.
|
|
2542
|
+
*/
|
|
2543
|
+
async moveSpeeches(input, options) {
|
|
2544
|
+
this.validator.validateMoveSpeeches(input, this.doc);
|
|
2545
|
+
this.doc.transact((draft) => {
|
|
2546
|
+
const ranges = mainTrackRanges(draft);
|
|
2547
|
+
const track = findLaneTrack(draft, "speech");
|
|
2548
|
+
for (const move of input.speeches) {
|
|
2549
|
+
const item = (track?.items ?? []).find((it) => it?.part_id === move.speech_id);
|
|
2550
|
+
if (item == null) continue;
|
|
2551
|
+
item.time_position = relativePositionForAbs(ranges, move.new_start_ms);
|
|
2552
|
+
item.fallback_abs_ms = move.new_start_ms;
|
|
2553
|
+
}
|
|
2554
|
+
}, audit("MoveSpeeches", input, options));
|
|
2555
|
+
}
|
|
2556
|
+
/** Change a speech's script. Re-TTS runs upstream; the regenerated parts arrive materialized (no cascade). */
|
|
2557
|
+
async changeSpeechScript(input, options) {
|
|
2558
|
+
this.validator.validateChangeSpeechScript(input, this.doc);
|
|
2559
|
+
this.doc.transact((draft) => {
|
|
2560
|
+
writeSpeechAssets(draft, input);
|
|
2561
|
+
}, audit("ChangeSpeechScript", input, options));
|
|
2562
|
+
}
|
|
2563
|
+
/** Change a speech's voice. Re-TTS runs upstream; the regenerated parts arrive materialized (no cascade). */
|
|
2564
|
+
async changeSpeechVoice(input, options) {
|
|
2565
|
+
this.validator.validateChangeSpeechVoice(input, this.doc);
|
|
2566
|
+
this.doc.transact((draft) => {
|
|
2567
|
+
writeSpeechAssets(draft, input);
|
|
2568
|
+
}, audit("ChangeSpeechVoice", input, options));
|
|
2569
|
+
}
|
|
2570
|
+
/**
|
|
2571
|
+
* Set the document BGM. The media's stable result arrives materialized.
|
|
2572
|
+
*
|
|
2573
|
+
* KNOWN GAP (non-blocking): when the BGM comes from the public library, the
|
|
2574
|
+
* Director must also register project-level stock media ownership. That is a
|
|
2575
|
+
* separate "register-only, no draft write" side effect Director does not yet
|
|
2576
|
+
* expose; until it does, a public-library BGM set here will not auto-appear in
|
|
2577
|
+
* the project media library. The document edit itself is complete and correct.
|
|
2578
|
+
* See `docs/projects/medeo-integration/results/phase-4-op-side-effect-classification.md`.
|
|
2579
|
+
*/
|
|
2580
|
+
async setBgm(input, options) {
|
|
2581
|
+
this.validator.validateSetBgm(input, this.doc);
|
|
2582
|
+
this.doc.transact((draft) => {
|
|
2583
|
+
const track = ensureLaneTrack(draft, "bgm");
|
|
2584
|
+
for (const item of track.items ?? []) if (item?.part_id != null) deletePart(draft, item.part_id);
|
|
2585
|
+
setPart(draft, input.bgm_id, { bgm: {
|
|
2586
|
+
id: input.bgm_id,
|
|
2587
|
+
kind: "bgm",
|
|
2588
|
+
audio_storage_key: input.audio_storage_key,
|
|
2589
|
+
volume: input.volume,
|
|
2590
|
+
origin_media_id: input.origin_media_id
|
|
2591
|
+
} });
|
|
2592
|
+
track.items = [{
|
|
2593
|
+
part_id: input.bgm_id,
|
|
2594
|
+
time_position: {
|
|
2595
|
+
mode: "absolute",
|
|
2596
|
+
offsetMs: 0
|
|
2597
|
+
},
|
|
2598
|
+
fallback_abs_ms: void 0
|
|
2599
|
+
}];
|
|
2600
|
+
}, audit("SetBgm", input, options));
|
|
2601
|
+
}
|
|
2602
|
+
/** Remove the document BGM; clears the bgm lane and removes the part. */
|
|
2603
|
+
async deleteBgm(input, options) {
|
|
2604
|
+
this.validator.validateDeleteBgm(input, this.doc);
|
|
2605
|
+
this.doc.transact((draft) => {
|
|
2606
|
+
const track = findLaneTrack(draft, "bgm");
|
|
2607
|
+
if (track == null) return;
|
|
2608
|
+
for (const item of track.items ?? []) if (item?.part_id != null) deletePart(draft, item.part_id);
|
|
2609
|
+
track.items = [];
|
|
2610
|
+
}, audit("DeleteBgm", input, options));
|
|
2611
|
+
}
|
|
2612
|
+
/** Toggle caption visibility (caption track `is_hidden`). */
|
|
2613
|
+
async setCaptionVisibility(input, options) {
|
|
2614
|
+
this.validator.validateSetCaptionVisibility(input, this.doc);
|
|
2615
|
+
this.doc.transact((draft) => {
|
|
2616
|
+
const track = findLaneTrack(draft, "caption");
|
|
2617
|
+
if (track == null) return;
|
|
2618
|
+
track.is_hidden = input.is_hidden;
|
|
2619
|
+
}, audit("SetCaptionVisibility", input, options));
|
|
2620
|
+
}
|
|
2621
|
+
/**
|
|
2622
|
+
* Set the document-wide caption style. GLOBAL (no `caption_id`): the patch is
|
|
2623
|
+
* merged onto EVERY caption part's `style`, mirroring the FE, which applies a
|
|
2624
|
+
* single style to all captions (`caption-style.ts:persistCaptionStylePatch`).
|
|
2625
|
+
*
|
|
2626
|
+
* Merge, not replace: only the fields present in the input overwrite the
|
|
2627
|
+
* caption's existing style; absent fields are carried forward. So a partial
|
|
2628
|
+
* patch ("recolor only") keeps the caption's font size. Pure document edit, no
|
|
2629
|
+
* cascade — positions are untouched.
|
|
2630
|
+
*/
|
|
2631
|
+
async setCaptionStyle(input, options) {
|
|
2632
|
+
this.validator.validateSetCaptionStyle(input, this.doc);
|
|
2633
|
+
this.doc.transact((draft) => {
|
|
2634
|
+
const library = draft.part_library ?? {};
|
|
2635
|
+
for (const partId of Object.keys(library)) {
|
|
2636
|
+
const caption = library[partId]?.caption;
|
|
2637
|
+
if (caption == null) continue;
|
|
2638
|
+
setPart(draft, partId, { caption: {
|
|
2639
|
+
...caption,
|
|
2640
|
+
kind: "caption",
|
|
2641
|
+
style: {
|
|
2642
|
+
...caption.style,
|
|
2643
|
+
...input
|
|
2644
|
+
}
|
|
2645
|
+
} });
|
|
2646
|
+
}
|
|
2647
|
+
}, audit("SetCaptionStyle", input, options));
|
|
2648
|
+
}
|
|
2649
|
+
/**
|
|
2650
|
+
* Resolve the main-track sequential layout from `source`. Callers inside a
|
|
2651
|
+
* `transact` MUST pass the live `draft` so item order reflects in-progress
|
|
2652
|
+
* mutations; the default `this.doc.snapshot()` is only committed state and is
|
|
2653
|
+
* correct for read-only callers outside a transaction. `readMainTrackItems` /
|
|
2654
|
+
* `readPartDurationMs` accept both an immer draft and a raw snapshot.
|
|
2655
|
+
*/
|
|
2656
|
+
computeMainTrackLayout(source = this.doc.snapshot()) {
|
|
2657
|
+
const items = readMainTrackItems(source);
|
|
2658
|
+
const layout = [];
|
|
2659
|
+
let cur = 0;
|
|
2660
|
+
for (let i = 0; i < items.length; i++) {
|
|
2661
|
+
const partId = items[i].part_id;
|
|
2662
|
+
if (partId == null) continue;
|
|
2663
|
+
const durationMs = readPartDurationMs(source, partId, 1e3);
|
|
2664
|
+
layout.push({
|
|
2665
|
+
index: i,
|
|
2666
|
+
part_id: partId,
|
|
2667
|
+
start_ms: cur,
|
|
2668
|
+
end_ms: cur + durationMs
|
|
2669
|
+
});
|
|
2670
|
+
cur += durationMs;
|
|
2671
|
+
}
|
|
2672
|
+
return layout;
|
|
2673
|
+
}
|
|
2674
|
+
indexForStartMs(layout, newStartMs, selfIndex) {
|
|
2675
|
+
const without = layout.filter((it) => it.index !== selfIndex);
|
|
2676
|
+
let cur = 0;
|
|
2677
|
+
let target = without.length;
|
|
2678
|
+
for (let i = 0; i < without.length; i++) {
|
|
2679
|
+
const it = without[i];
|
|
2680
|
+
const durationMs = it.end_ms - it.start_ms;
|
|
2681
|
+
if (newStartMs <= cur + durationMs / 2) {
|
|
2682
|
+
target = i;
|
|
2683
|
+
break;
|
|
2684
|
+
}
|
|
2685
|
+
cur += durationMs;
|
|
2686
|
+
}
|
|
2687
|
+
return Math.max(0, Math.min(target, layout.length - 1));
|
|
2688
|
+
}
|
|
2689
|
+
computeAddInsertIndex(input) {
|
|
2690
|
+
const items = readMainTrackItems(this.doc.snapshot());
|
|
2691
|
+
if (input.before_clip_id != null) {
|
|
2692
|
+
const idx = items.findIndex((it) => it.part_id === input.before_clip_id);
|
|
2693
|
+
return idx < 0 ? items.length : idx;
|
|
2694
|
+
}
|
|
2695
|
+
if (input.after_clip_id != null) {
|
|
2696
|
+
const idx = items.findIndex((it) => it.part_id === input.after_clip_id);
|
|
2697
|
+
return idx < 0 ? items.length : idx + 1;
|
|
2698
|
+
}
|
|
2699
|
+
const firstStart = input.clips[0]?.start_ms;
|
|
2700
|
+
if (firstStart == null) return items.length;
|
|
2701
|
+
const snapshot = this.doc.snapshot();
|
|
2702
|
+
let cur = 0;
|
|
2703
|
+
for (let i = 0; i < items.length; i++) {
|
|
2704
|
+
const partId = items[i].part_id;
|
|
2705
|
+
const durationMs = partId == null ? 1e3 : readPartDurationMs(snapshot, partId, 1e3);
|
|
2706
|
+
if (firstStart <= cur + durationMs / 2) return i;
|
|
2707
|
+
cur += durationMs;
|
|
2708
|
+
}
|
|
2709
|
+
return items.length;
|
|
2710
|
+
}
|
|
2711
|
+
};
|
|
2712
|
+
function audit(kind, payload, options) {
|
|
2713
|
+
return {
|
|
2714
|
+
kind,
|
|
2715
|
+
payload,
|
|
2716
|
+
intent: options?.intent ?? null
|
|
2717
|
+
};
|
|
2718
|
+
}
|
|
2719
|
+
function mainTrackRow(draft) {
|
|
2720
|
+
return (draft.tracks ?? []).find((t) => t?.parts_kind === "video_clip");
|
|
2721
|
+
}
|
|
2722
|
+
/**
|
|
2723
|
+
* Locate the main (video_clip) track in the single `tracks` list, minting an
|
|
2724
|
+
* empty one in lane-stacking order if absent (reference/17 §4: lane =
|
|
2725
|
+
* `parts_kind`). Used by ops that add the first clips into an empty document.
|
|
2726
|
+
* Delegates to `ensureLaneTrack` so the caption → main → speech/bgm ordering is
|
|
2727
|
+
* enforced in one place.
|
|
2728
|
+
*/
|
|
2729
|
+
function ensureMainTrack(draft) {
|
|
2730
|
+
return ensureLaneTrack(draft, "video_clip");
|
|
2731
|
+
}
|
|
2732
|
+
function mainTrackItems(draft) {
|
|
2733
|
+
const track = mainTrackRow(draft);
|
|
2734
|
+
if (track == null) return void 0;
|
|
2735
|
+
track.items ??= [];
|
|
2736
|
+
return track.items;
|
|
2737
|
+
}
|
|
2738
|
+
function moveItem(items, from, to) {
|
|
2739
|
+
if (from === to) return;
|
|
2740
|
+
const [moved] = items.splice(from, 1);
|
|
2741
|
+
items.splice(to, 0, moved);
|
|
2742
|
+
}
|
|
2743
|
+
function setPart(draft, partId, part) {
|
|
2744
|
+
draft.part_library ??= {};
|
|
2745
|
+
draft.part_library[partId] = partUnionToDraft(part);
|
|
2746
|
+
}
|
|
2747
|
+
function deletePart(draft, partId) {
|
|
2748
|
+
if (draft.part_library != null) delete draft.part_library[partId];
|
|
2749
|
+
}
|
|
2750
|
+
/**
|
|
2751
|
+
* The validated speed-shift input already matches the domain `SpeedShift` shape
|
|
2752
|
+
* (`category` enum + `config` linear/curve union, mirroring the IDL); this just
|
|
2753
|
+
* narrows `null` (clear speed back to 1×) to `undefined`.
|
|
2754
|
+
*/
|
|
2755
|
+
function normalizeSpeedShift(input) {
|
|
2756
|
+
return input ?? void 0;
|
|
2757
|
+
}
|
|
2758
|
+
/**
|
|
2759
|
+
* Write a materialized speech subtree into the draft: upsert each speech +
|
|
2760
|
+
* caption part, and insert/update their lane items with `anchored` `time_position`
|
|
2761
|
+
* facts taken verbatim from the input (RFC 02 §4) — speech anchors to its host
|
|
2762
|
+
* video clip (`anchor_part_id` + `offset_ms`); caption anchors to its speech
|
|
2763
|
+
* (offset = caption part `start_ms`). No absolute time is written; the
|
|
2764
|
+
* projection derives it on read. On re-TTS the speech id is preserved, so an
|
|
2765
|
+
* existing item is re-positioned rather than duplicated.
|
|
2766
|
+
*/
|
|
2767
|
+
function writeSpeechAssets(draft, assets) {
|
|
2768
|
+
const captionById = new Map(assets.captions.map((c) => [c.caption_id, c]));
|
|
2769
|
+
const speechTrack = ensureLaneTrack(draft, "speech");
|
|
2770
|
+
const captionTrack = ensureLaneTrack(draft, "caption");
|
|
2771
|
+
speechTrack.items ??= [];
|
|
2772
|
+
captionTrack.items ??= [];
|
|
2773
|
+
const ranges = mainTrackRanges(draft);
|
|
2774
|
+
const hostStartMs = (anchorPartId) => ranges.find((r) => r.partId === anchorPartId)?.startMs;
|
|
2775
|
+
for (const speech of assets.speeches) {
|
|
2776
|
+
const priorCaptionIds = draft.part_library?.[speech.speech_id]?.speech?.caption_ids ?? [];
|
|
2777
|
+
const nextCaptionIds = new Set(speech.caption_ids);
|
|
2778
|
+
const removedCaptionIds = priorCaptionIds.filter((id) => id != null && !nextCaptionIds.has(id));
|
|
2779
|
+
for (const captionId of removedCaptionIds) deletePart(draft, captionId);
|
|
2780
|
+
if (removedCaptionIds.length > 0) {
|
|
2781
|
+
const removed = new Set(removedCaptionIds);
|
|
2782
|
+
captionTrack.items = captionTrack.items.filter((it) => it?.part_id == null || !removed.has(it.part_id));
|
|
2783
|
+
}
|
|
2784
|
+
const priorSpeech = draft.part_library?.[speech.speech_id]?.speech;
|
|
2785
|
+
setPart(draft, speech.speech_id, { speech: {
|
|
2786
|
+
...priorSpeech,
|
|
2787
|
+
id: speech.speech_id,
|
|
2788
|
+
kind: "speech",
|
|
2789
|
+
media_duration_ms: speech.duration_ms,
|
|
2790
|
+
audio_script: speech.audio_script,
|
|
2791
|
+
volume: speech.volume,
|
|
2792
|
+
audio_storage_key: speech.audio_storage_key,
|
|
2793
|
+
origin_speech_id: speech.origin_speech_id,
|
|
2794
|
+
voice: speech.voice,
|
|
2795
|
+
caption_ids: speech.caption_ids
|
|
2796
|
+
} });
|
|
2797
|
+
const hostStart = hostStartMs(speech.anchor_part_id);
|
|
2798
|
+
const speechAbs = hostStart == null ? void 0 : hostStart + speech.offset_ms;
|
|
2799
|
+
placeRelative(speechTrack.items, speech.speech_id, speech.anchor_part_id, speech.offset_ms, speechAbs);
|
|
2800
|
+
for (const captionId of speech.caption_ids) {
|
|
2801
|
+
const caption = captionById.get(captionId);
|
|
2802
|
+
if (caption == null) continue;
|
|
2803
|
+
const priorCaption = draft.part_library?.[captionId]?.caption;
|
|
2804
|
+
setPart(draft, captionId, { caption: {
|
|
2805
|
+
...priorCaption,
|
|
2806
|
+
id: captionId,
|
|
2807
|
+
kind: "caption",
|
|
2808
|
+
initial_duration_ms: caption.duration_ms,
|
|
2809
|
+
speech_part_id: speech.speech_id,
|
|
2810
|
+
text: caption.text,
|
|
2811
|
+
start_ms: caption.start_ms
|
|
2812
|
+
} });
|
|
2813
|
+
const captionAbs = speechAbs == null ? void 0 : speechAbs + caption.start_ms;
|
|
2814
|
+
placeRelative(captionTrack.items, captionId, speech.speech_id, caption.start_ms, captionAbs);
|
|
2815
|
+
}
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
/**
|
|
2819
|
+
* Insert a lane item with an `anchored` time position fact, or re-position it if
|
|
2820
|
+
* already present. `fallbackAbsMs` is the orphan-recovery snapshot (RFC 02
|
|
2821
|
+
* §11.1): the item's absolute landing the caller resolved from its anchor, so a
|
|
2822
|
+
* freshly-created anchored item can still be recovered if its anchor is later
|
|
2823
|
+
* concurrently deleted. Pass `undefined` only when the anchor's absolute
|
|
2824
|
+
* position cannot be resolved (no host clip yet).
|
|
2825
|
+
*/
|
|
2826
|
+
function placeRelative(items, partId, anchorPartId, offsetMs, fallbackAbsMs) {
|
|
2827
|
+
const timePosition = {
|
|
2828
|
+
mode: "anchored",
|
|
2829
|
+
anchorPartId,
|
|
2830
|
+
offsetMs: Math.max(0, Math.round(offsetMs))
|
|
2831
|
+
};
|
|
2832
|
+
const existing = items.find((it) => it?.part_id === partId);
|
|
2833
|
+
if (existing != null) {
|
|
2834
|
+
existing.time_position = timePosition;
|
|
2835
|
+
if (fallbackAbsMs != null) existing.fallback_abs_ms = fallbackAbsMs;
|
|
2836
|
+
return;
|
|
2837
|
+
}
|
|
2838
|
+
items.push({
|
|
2839
|
+
part_id: partId,
|
|
2840
|
+
time_position: timePosition,
|
|
2841
|
+
fallback_abs_ms: fallbackAbsMs
|
|
2842
|
+
});
|
|
2843
|
+
}
|
|
2844
|
+
/**
|
|
2845
|
+
* After the main-track flow order changes (move / reorder), re-parent each
|
|
2846
|
+
* anchored speech to the clip its *preserved* absolute landing now falls in
|
|
2847
|
+
* (RFC 02 §9.1). `beforeRanges` is the pre-change main-track layout, used only
|
|
2848
|
+
* to recover each speech's current absolute time; the new layout is read fresh.
|
|
2849
|
+
* Writes only the affected speech `position` facts — a single forward pass, not
|
|
2850
|
+
* a cascade. Captions follow their speech (relative to it), so they need no
|
|
2851
|
+
* rewrite.
|
|
2852
|
+
*/
|
|
2853
|
+
function reparentSpeechesAfterMainTrackChange(draft, beforeRanges) {
|
|
2854
|
+
const speechTrack = findLaneTrack(draft, "speech");
|
|
2855
|
+
if (speechTrack?.items == null) return;
|
|
2856
|
+
const beforeStart = new Map(beforeRanges.map((r) => [r.partId, r.startMs]));
|
|
2857
|
+
const afterRanges = mainTrackRanges(draft);
|
|
2858
|
+
for (const item of speechTrack.items) {
|
|
2859
|
+
const timePosition = item?.time_position;
|
|
2860
|
+
if (timePosition?.mode !== "anchored") continue;
|
|
2861
|
+
const hostStart = beforeStart.get(timePosition.anchorPartId);
|
|
2862
|
+
if (hostStart == null) continue;
|
|
2863
|
+
const currentAbs = hostStart + timePosition.offsetMs;
|
|
2864
|
+
item.time_position = relativePositionForAbs(afterRanges, currentAbs);
|
|
2865
|
+
item.fallback_abs_ms = currentAbs;
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
/**
|
|
2869
|
+
* Refresh the orphan-recovery snapshot (`fallback_abs_ms`, RFC 02 §11.1) of
|
|
2870
|
+
* every `anchored` speech from the current main-track layout, keeping the
|
|
2871
|
+
* `time_position` facts untouched. Called after an op that shifts clip positions
|
|
2872
|
+
* without re-parenting (e.g. re-trim), so a later orphan recovery uses an
|
|
2873
|
+
* absolute landing that matches the post-edit layout, not a stale one. Speeches
|
|
2874
|
+
* whose anchor clip is absent are left as-is (recovery handles them).
|
|
2875
|
+
*/
|
|
2876
|
+
function refreshAnchoredFallbackAbs(draft) {
|
|
2877
|
+
const speechTrack = findLaneTrack(draft, "speech");
|
|
2878
|
+
if (speechTrack?.items == null) return;
|
|
2879
|
+
const startByPart = new Map(mainTrackRanges(draft).map((r) => [r.partId, r.startMs]));
|
|
2880
|
+
for (const item of speechTrack.items) {
|
|
2881
|
+
const timePosition = item?.time_position;
|
|
2882
|
+
if (timePosition?.mode !== "anchored") continue;
|
|
2883
|
+
const hostStart = startByPart.get(timePosition.anchorPartId);
|
|
2884
|
+
if (hostStart == null) continue;
|
|
2885
|
+
item.fallback_abs_ms = hostStart + timePosition.offsetMs;
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
/**
|
|
2889
|
+
* Remove a speech part, its captions, and all their lane items (RFC 02 §9.2
|
|
2890
|
+
* subtree). The cascade basis is the anchored `time_position`, not the speech's
|
|
2891
|
+
* `caption_ids` (reference/17 §6): a caption is deleted iff it is `anchored` to
|
|
2892
|
+
* this speech (`time_position.anchorPartId === speechId`). `caption_ids` stays as
|
|
2893
|
+
* a resource-intrinsic fact (reference/17 principle 4) but no longer drives
|
|
2894
|
+
* deletion — "what to delete" is decoupled from "what the resource is".
|
|
2895
|
+
*/
|
|
2896
|
+
function deleteSpeechSubtree(draft, speechId) {
|
|
2897
|
+
const captionTrack = findLaneTrack(draft, "caption");
|
|
2898
|
+
const anchoredCaptionIds = new Set((captionTrack?.items ?? []).filter((it) => it?.time_position?.mode === "anchored" && it.time_position.anchorPartId === speechId).map((it) => it.part_id).filter((id) => id != null));
|
|
2899
|
+
deletePart(draft, speechId);
|
|
2900
|
+
for (const captionId of anchoredCaptionIds) deletePart(draft, captionId);
|
|
2901
|
+
const speechTrack = findLaneTrack(draft, "speech");
|
|
2902
|
+
if (speechTrack?.items != null) speechTrack.items = speechTrack.items.filter((it) => it?.part_id !== speechId);
|
|
2903
|
+
if (captionTrack?.items != null) captionTrack.items = captionTrack.items.filter((it) => it?.part_id == null || !anchoredCaptionIds.has(it.part_id));
|
|
2904
|
+
}
|
|
2905
|
+
/**
|
|
2906
|
+
* Detach the direct anchored children of a video clip (reference/17 §6 `detach`):
|
|
2907
|
+
* each speech anchored to `clipId` is re-pinned to `{ mode:'absolute' }` at its
|
|
2908
|
+
* current projected landing, so it stays on the timeline once the clip is gone.
|
|
2909
|
+
* Computed while the anchor chain is intact (call before removing the clip), so
|
|
2910
|
+
* the absolute landing is always resolvable — no `fallback_abs_ms` read needed.
|
|
2911
|
+
*
|
|
2912
|
+
* Only the *direct* children detach: a speech's captions anchor to the speech
|
|
2913
|
+
* (not the clip), so they keep following the speech and need no rewrite.
|
|
2914
|
+
*/
|
|
2915
|
+
function detachAnchoredChildren(draft, clipId) {
|
|
2916
|
+
const speechTrack = findLaneTrack(draft, "speech");
|
|
2917
|
+
if (speechTrack?.items == null) return;
|
|
2918
|
+
const clipStart = new Map(mainTrackRanges(draft).map((r) => [r.partId, r.startMs])).get(clipId);
|
|
2919
|
+
for (const item of speechTrack.items) {
|
|
2920
|
+
const timePosition = item?.time_position;
|
|
2921
|
+
if (timePosition?.mode !== "anchored" || timePosition.anchorPartId !== clipId) continue;
|
|
2922
|
+
const abs = (clipStart ?? item.fallback_abs_ms ?? 0) + timePosition.offsetMs;
|
|
2923
|
+
item.time_position = {
|
|
2924
|
+
mode: "absolute",
|
|
2925
|
+
offsetMs: Math.max(0, Math.round(abs))
|
|
2926
|
+
};
|
|
2927
|
+
item.fallback_abs_ms = void 0;
|
|
2928
|
+
}
|
|
2929
|
+
}
|
|
2930
|
+
/** Delete every speech (with its captions) anchored to the given video clip (RFC 02 §9.2). */
|
|
2931
|
+
function deleteAnchoredSubtree(draft, clipId) {
|
|
2932
|
+
const anchored = (findLaneTrack(draft, "speech")?.items ?? []).filter((it) => it?.time_position?.mode === "anchored" && it.time_position.anchorPartId === clipId).map((it) => it.part_id).filter((id) => id != null);
|
|
2933
|
+
for (const speechId of anchored) deleteSpeechSubtree(draft, speechId);
|
|
2934
|
+
}
|
|
2935
|
+
//#endregion
|
|
2936
|
+
//#region src/editor/types.ts
|
|
2937
|
+
/** Runtime list of the frozen, implemented kinds (for guards / introspection). */
|
|
2938
|
+
const IMPLEMENTED_SEMANTIC_OP_KINDS = [
|
|
2939
|
+
"MoveVideoClips",
|
|
2940
|
+
"DeleteVideoClips",
|
|
2941
|
+
"AddVideoClips",
|
|
2942
|
+
"AdjustVideoClipVolume",
|
|
2943
|
+
"SetVideoClipSpeedShift",
|
|
2944
|
+
"ReplaceVideoClipContent",
|
|
2945
|
+
"AdjustVideoClipDuration",
|
|
2946
|
+
"AddSpeeches",
|
|
2947
|
+
"DeleteSpeeches",
|
|
2948
|
+
"MoveSpeeches",
|
|
2949
|
+
"ChangeSpeechScript",
|
|
2950
|
+
"ChangeSpeechVoice",
|
|
2951
|
+
"AdjustSpeechVolume",
|
|
2952
|
+
"SetCaptionVisibility",
|
|
2953
|
+
"SetCaptionStyle",
|
|
2954
|
+
"SetBgm",
|
|
2955
|
+
"DeleteBgm",
|
|
2956
|
+
"AdjustBgmVolume"
|
|
2957
|
+
];
|
|
2958
|
+
function isImplementedSemanticOpKind(kind) {
|
|
2959
|
+
return IMPLEMENTED_SEMANTIC_OP_KINDS.includes(kind);
|
|
2960
|
+
}
|
|
2961
|
+
//#endregion
|
|
2962
|
+
//#region src/storage/medeo-http-doc-storage.ts
|
|
2963
|
+
/**
|
|
2964
|
+
* SSE-backed {@link Connection} for the mengine-server document stream.
|
|
2965
|
+
*
|
|
2966
|
+
* It owns the live SSE read loop and reflects its lifecycle as connection
|
|
2967
|
+
* status: `connecting` while (re)establishing, `connected` once the stream is
|
|
2968
|
+
* open, back to `connecting` on a drop (auto-reconnect), `closed` on
|
|
2969
|
+
* `disconnect`. Reporting a drop as a status change is the whole point — the
|
|
2970
|
+
* synchronizer watches `onStatusChanged` and, on any change, tears down and
|
|
2971
|
+
* re-runs its connect cycle, which re-issues `getDocDiff(doc.version())` and so
|
|
2972
|
+
* recovers whatever the stream missed while it was down. Recovery therefore
|
|
2973
|
+
* lives in the synchronizer (keyed on the real doc version), not here.
|
|
2974
|
+
*
|
|
2975
|
+
* Each SSE frame's updates are forwarded verbatim via `onUpdate`; this
|
|
2976
|
+
* connection keeps no version cursor and does no catch-up of its own. Dedup is
|
|
2977
|
+
* unnecessary because `LoroDoc.import` is idempotent by OpId/VV.
|
|
2978
|
+
*/
|
|
2979
|
+
var SseConnection = class {
|
|
2980
|
+
client;
|
|
2981
|
+
onUpdate;
|
|
2982
|
+
reconnectDelayMs;
|
|
2983
|
+
inner = void 0;
|
|
2984
|
+
event = new EventBus();
|
|
2985
|
+
streamTask = null;
|
|
2986
|
+
_status = "idle";
|
|
2987
|
+
_error;
|
|
2988
|
+
constructor(client, onUpdate, reconnectDelayMs) {
|
|
2989
|
+
this.client = client;
|
|
2990
|
+
this.onUpdate = onUpdate;
|
|
2991
|
+
this.reconnectDelayMs = reconnectDelayMs;
|
|
2992
|
+
}
|
|
2993
|
+
get status() {
|
|
2994
|
+
return this._status;
|
|
2995
|
+
}
|
|
2996
|
+
get error() {
|
|
2997
|
+
return this._error;
|
|
2998
|
+
}
|
|
2999
|
+
connect() {
|
|
3000
|
+
if (this.streamTask != null) return;
|
|
3001
|
+
const task = Task.spawn((scope) => this.runStreamLoop(scope.signal));
|
|
3002
|
+
this.streamTask = task;
|
|
3003
|
+
const release = () => {
|
|
3004
|
+
if (this.streamTask === task) this.streamTask = null;
|
|
3005
|
+
};
|
|
3006
|
+
task.then(release, release);
|
|
3007
|
+
}
|
|
3008
|
+
disconnect() {
|
|
3009
|
+
this.streamTask?.cancel();
|
|
3010
|
+
this.streamTask = null;
|
|
3011
|
+
this.setStatus("closed");
|
|
3012
|
+
}
|
|
3013
|
+
waitForConnected() {
|
|
3014
|
+
return new Task((resolve, reject, scope) => {
|
|
3015
|
+
if (this._status === "connected") {
|
|
3016
|
+
resolve();
|
|
3017
|
+
return;
|
|
3018
|
+
}
|
|
3019
|
+
const off = this.onStatusChanged((status, error) => {
|
|
3020
|
+
if (status === "connected") {
|
|
3021
|
+
off();
|
|
3022
|
+
resolve();
|
|
3023
|
+
} else if (status === "closed") {
|
|
3024
|
+
off();
|
|
3025
|
+
reject(error ?? /* @__PURE__ */ new Error("SSE connection closed"));
|
|
3026
|
+
}
|
|
3027
|
+
});
|
|
3028
|
+
scope.disposer.add(off);
|
|
3029
|
+
});
|
|
3030
|
+
}
|
|
3031
|
+
onStatusChanged(cb) {
|
|
3032
|
+
return this.event.on("statusChanged", ({ status, error }) => cb(status, error));
|
|
3033
|
+
}
|
|
3034
|
+
setStatus(status, error) {
|
|
3035
|
+
if (this._status === status && this._error === error) return;
|
|
3036
|
+
this._status = status;
|
|
3037
|
+
this._error = error;
|
|
3038
|
+
this.event.emit("statusChanged", {
|
|
3039
|
+
status,
|
|
3040
|
+
error
|
|
3041
|
+
});
|
|
3042
|
+
}
|
|
3043
|
+
async runStreamLoop(signal) {
|
|
3044
|
+
while (!signal.aborted) {
|
|
3045
|
+
this.setStatus("connecting");
|
|
3046
|
+
try {
|
|
3047
|
+
await readMengineEventStream({
|
|
3048
|
+
client: this.client,
|
|
3049
|
+
signal,
|
|
3050
|
+
onOpen: () => this.setStatus("connected"),
|
|
3051
|
+
onUpdate: (event) => {
|
|
3052
|
+
for (const update of event.updates) this.onUpdate(update);
|
|
3053
|
+
}
|
|
3054
|
+
});
|
|
3055
|
+
} catch (error) {
|
|
3056
|
+
if (signal.aborted) break;
|
|
3057
|
+
this.setStatus("connecting", error instanceof Error ? error : new Error(String(error)));
|
|
3058
|
+
}
|
|
3059
|
+
if (signal.aborted) break;
|
|
3060
|
+
try {
|
|
3061
|
+
await Task.delay(this.reconnectDelayMs).abortOn(signal);
|
|
3062
|
+
} catch {
|
|
3063
|
+
break;
|
|
3064
|
+
}
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
};
|
|
3068
|
+
/**
|
|
3069
|
+
* Adapts the mengine-server HTTP/SSE protocol to the engine `DocStorage`
|
|
3070
|
+
* contract so `ClientServerSynchronizer` can treat it as a remote peer.
|
|
3071
|
+
*
|
|
3072
|
+
* Deliberately thin (mirrors the socket `DocStorage` in the playground): it
|
|
3073
|
+
* forwards live SSE updates and exposes a version-vector diff, and keeps NO
|
|
3074
|
+
* sync state of its own.
|
|
3075
|
+
*
|
|
3076
|
+
* - `getDocDiff(docId, knownVersion)` pulls the server-computed VV-diff via
|
|
3077
|
+
* `GET /sync?from=<vv>` — the synchronizer passes the real `doc.version()`, so
|
|
3078
|
+
* the response carries exactly the ops the doc is missing. `getDoc` (full
|
|
3079
|
+
* `/snapshot`) stays for cold start, when the caller holds no version yet.
|
|
3080
|
+
* - `pushDocUpdate` forwards a Loro update; the server appends it.
|
|
3081
|
+
* - `subscribeDocUpdate` registers a callback for live SSE updates. It does no
|
|
3082
|
+
* catch-up and keeps no cursor: after an SSE drop the connection reports a
|
|
3083
|
+
* status change, and the synchronizer re-runs its cycle to catch up via
|
|
3084
|
+
* `getDocDiff(doc.version())`. `LoroDoc.import` is idempotent (OpId/VV), so
|
|
3085
|
+
* re-forwarded or echoed updates are harmless.
|
|
3086
|
+
*
|
|
3087
|
+
* It is bound to a single `docId` because `MengineHttpClient` is per-document.
|
|
3088
|
+
*/
|
|
3089
|
+
var MedeoHttpDocStorage = class {
|
|
3090
|
+
options;
|
|
3091
|
+
connection;
|
|
3092
|
+
client;
|
|
3093
|
+
docId;
|
|
3094
|
+
events = new EventBus();
|
|
3095
|
+
constructor(options) {
|
|
3096
|
+
this.options = options;
|
|
3097
|
+
this.client = options.client;
|
|
3098
|
+
this.docId = options.docId;
|
|
3099
|
+
this.connection = new SseConnection(this.client, (update) => this.emitUpdate(update), options.sseReconnectDelayMs ?? 500);
|
|
3100
|
+
}
|
|
3101
|
+
get isReadonly() {
|
|
3102
|
+
return this.options.readonlyMode ?? false;
|
|
3103
|
+
}
|
|
3104
|
+
async getDoc(docId) {
|
|
3105
|
+
this.assertDocId(docId);
|
|
3106
|
+
const snapshot = await this.client.fetchSnapshot();
|
|
3107
|
+
const now = /* @__PURE__ */ new Date();
|
|
3108
|
+
return {
|
|
3109
|
+
docId,
|
|
3110
|
+
data: base64ToBytes(snapshot.snapshot),
|
|
3111
|
+
createdAt: now,
|
|
3112
|
+
updatedAt: now
|
|
3113
|
+
};
|
|
3114
|
+
}
|
|
3115
|
+
async getDocDiff(docId, knownVersion) {
|
|
3116
|
+
this.assertDocId(docId);
|
|
3117
|
+
const response = await this.client.sync(knownVersion);
|
|
3118
|
+
return {
|
|
3119
|
+
docId,
|
|
3120
|
+
missing: base64ToBytes(response.update),
|
|
3121
|
+
version: base64ToBytes(response.server_vv)
|
|
3122
|
+
};
|
|
3123
|
+
}
|
|
3124
|
+
async pushDocUpdate(update, _origin) {
|
|
3125
|
+
this.assertDocId(update.docId);
|
|
3126
|
+
if (this.isReadonly || update.data.byteLength === 0) return;
|
|
3127
|
+
await this.client.pushUpdate(update.data);
|
|
3128
|
+
}
|
|
3129
|
+
async deleteDoc(_docId) {
|
|
3130
|
+
throw new Error("MedeoHttpDocStorage does not support deleteDoc");
|
|
3131
|
+
}
|
|
3132
|
+
subscribeDocUpdate(callback) {
|
|
3133
|
+
return this.events.on("update", ({ update, origin }) => callback(update, origin));
|
|
3134
|
+
}
|
|
3135
|
+
assertDocId(docId) {
|
|
3136
|
+
if (docId !== this.docId) throw new Error(`MedeoHttpDocStorage is bound to ${this.docId}, received ${docId}`);
|
|
3137
|
+
}
|
|
3138
|
+
emitUpdate(base64Update) {
|
|
3139
|
+
this.events.emit("update", {
|
|
3140
|
+
update: {
|
|
3141
|
+
docId: this.docId,
|
|
3142
|
+
data: base64ToBytes(base64Update)
|
|
3143
|
+
},
|
|
3144
|
+
origin: void 0
|
|
3145
|
+
});
|
|
3146
|
+
}
|
|
3147
|
+
};
|
|
3148
|
+
//#endregion
|
|
3149
|
+
//#region src/storage/memory-doc-storage.ts
|
|
3150
|
+
/**
|
|
3151
|
+
* Runtime-neutral local `DocStorage` backed by in-process memory.
|
|
3152
|
+
*
|
|
3153
|
+
* `IndexedDBDocStorage` is the browser-side local storage, but it requires
|
|
3154
|
+
* `indexedDB`/`idb`, which is absent in Node (FE/agent tests, unit tests, SSR).
|
|
3155
|
+
* The mengine runtime injects this implementation as the local peer in those
|
|
3156
|
+
* environments so the same `DocManager` + `ClientServerSynchronizer` wiring
|
|
3157
|
+
* works without a browser. It mirrors the merge-on-read and update-sequence
|
|
3158
|
+
* behavior of `IndexedDBDocStorage` so sync semantics are identical.
|
|
3159
|
+
*/
|
|
3160
|
+
var MemoryDocStorage = class extends BaseDocStorage {
|
|
3161
|
+
connection = new DummyConnection();
|
|
3162
|
+
entries = /* @__PURE__ */ new Map();
|
|
3163
|
+
constructor(options = {}) {
|
|
3164
|
+
super(options);
|
|
3165
|
+
}
|
|
3166
|
+
async pushDocUpdate(update, origin) {
|
|
3167
|
+
const entry = this.entry(update.docId);
|
|
3168
|
+
if (entry.snapshot == null && entry.updates.length === 0) {
|
|
3169
|
+
const now = this.now();
|
|
3170
|
+
entry.snapshot = {
|
|
3171
|
+
docId: update.docId,
|
|
3172
|
+
data: update.data,
|
|
3173
|
+
createdAt: now,
|
|
3174
|
+
updatedAt: now
|
|
3175
|
+
};
|
|
3176
|
+
} else {
|
|
3177
|
+
entry.seq += 1;
|
|
3178
|
+
entry.updates.push({
|
|
3179
|
+
docId: update.docId,
|
|
3180
|
+
seq: entry.seq,
|
|
3181
|
+
data: update.data,
|
|
3182
|
+
createdAt: this.now()
|
|
3183
|
+
});
|
|
3184
|
+
}
|
|
3185
|
+
this.event.emit("update", {
|
|
3186
|
+
update: {
|
|
3187
|
+
docId: update.docId,
|
|
3188
|
+
data: update.data
|
|
3189
|
+
},
|
|
3190
|
+
origin
|
|
3191
|
+
});
|
|
3192
|
+
}
|
|
3193
|
+
async deleteDoc(docId) {
|
|
3194
|
+
this.entries.delete(docId);
|
|
3195
|
+
}
|
|
3196
|
+
async getDocSnapshot(docId) {
|
|
3197
|
+
return this.entries.get(docId)?.snapshot ?? null;
|
|
3198
|
+
}
|
|
3199
|
+
async setDocSnapshot(snapshot) {
|
|
3200
|
+
const entry = this.entry(snapshot.docId);
|
|
3201
|
+
if (entry.snapshot == null || entry.snapshot.updatedAt <= snapshot.updatedAt) entry.snapshot = snapshot;
|
|
3202
|
+
return true;
|
|
3203
|
+
}
|
|
3204
|
+
async getDocUpdates(docId) {
|
|
3205
|
+
return [...this.entries.get(docId)?.updates ?? []];
|
|
3206
|
+
}
|
|
3207
|
+
async markUpdatesMerged(docId, updates) {
|
|
3208
|
+
const entry = this.entries.get(docId);
|
|
3209
|
+
if (!entry) return 0;
|
|
3210
|
+
const merged = new Set(updates.map((update) => update.seq));
|
|
3211
|
+
entry.updates = entry.updates.filter((update) => !merged.has(update.seq));
|
|
3212
|
+
return merged.size;
|
|
3213
|
+
}
|
|
3214
|
+
entry(docId) {
|
|
3215
|
+
let entry = this.entries.get(docId);
|
|
3216
|
+
if (!entry) {
|
|
3217
|
+
entry = {
|
|
3218
|
+
snapshot: null,
|
|
3219
|
+
updates: [],
|
|
3220
|
+
seq: 0
|
|
3221
|
+
};
|
|
3222
|
+
this.entries.set(docId, entry);
|
|
3223
|
+
}
|
|
3224
|
+
return entry;
|
|
3225
|
+
}
|
|
3226
|
+
now() {
|
|
3227
|
+
return /* @__PURE__ */ new Date();
|
|
3228
|
+
}
|
|
3229
|
+
};
|
|
3230
|
+
//#endregion
|
|
3231
|
+
//#region src/session/mengine-doc-session.ts
|
|
3232
|
+
/**
|
|
3233
|
+
* A live editing session for one Medeo document — the single entry point clients
|
|
3234
|
+
* (FE draft driver, Agent, embeds) use to open, edit, and observe a document.
|
|
3235
|
+
*
|
|
3236
|
+
* It assembles the engine sync stack around one Medeo document:
|
|
3237
|
+
*
|
|
3238
|
+
* local DocStorage ─┐
|
|
3239
|
+
* ├─ ClientServerSynchronizer ── DocManager ── LoroDoc
|
|
3240
|
+
* MedeoHttpDocStorage┘ │
|
|
3241
|
+
* (remote = Rust mengine-server) ▼
|
|
3242
|
+
* MirrorVideoDocumentAdapter + SemanticEditor
|
|
3243
|
+
*
|
|
3244
|
+
* `DocManager` owns the `LoroDoc`: local edits committed on the adapter are
|
|
3245
|
+
* picked up via `subscribeLocalUpdates`, saved to local storage, then pushed to
|
|
3246
|
+
* the server by the synchronizer. Remote SSE updates flow server → synchronizer
|
|
3247
|
+
* → local storage → manager → `LoroDoc`. A single `LoroDoc.subscribe` turns any
|
|
3248
|
+
* resulting change into a snapshot event, so callers never track update ids by
|
|
3249
|
+
* hand.
|
|
3250
|
+
*
|
|
3251
|
+
* Replaces the per-consumer hand-written pull/SSE loops that previously lived in
|
|
3252
|
+
* the standalone client session and `MengineDraftDriver` with one verified path.
|
|
3253
|
+
*/
|
|
3254
|
+
var MengineDocSession = class {
|
|
3255
|
+
options;
|
|
3256
|
+
docId;
|
|
3257
|
+
local;
|
|
3258
|
+
server;
|
|
3259
|
+
synchronizer;
|
|
3260
|
+
manager;
|
|
3261
|
+
events = new EventBus();
|
|
3262
|
+
disposables = new DisposableSet();
|
|
3263
|
+
adapterValue = null;
|
|
3264
|
+
editorValue = null;
|
|
3265
|
+
started = false;
|
|
3266
|
+
constructor(options) {
|
|
3267
|
+
this.options = options;
|
|
3268
|
+
this.docId = options.docId;
|
|
3269
|
+
this.local = options.localStorage ?? new MemoryDocStorage();
|
|
3270
|
+
this.server = new MedeoHttpDocStorage({
|
|
3271
|
+
docId: options.docId,
|
|
3272
|
+
client: options.client,
|
|
3273
|
+
sseReconnectDelayMs: options.sseReconnectDelayMs
|
|
3274
|
+
});
|
|
3275
|
+
this.synchronizer = new ClientServerSynchronizer(this.local, this.server);
|
|
3276
|
+
this.manager = new DocManager(this.local, this.synchronizer);
|
|
3277
|
+
}
|
|
3278
|
+
/**
|
|
3279
|
+
* The editor for local edits. Each op method validates, writes, and commits
|
|
3280
|
+
* itself as one SemanticOp (single commit carrying its audit message), so
|
|
3281
|
+
* callers just call `session.editor.someOp(...)` — there is no separate commit
|
|
3282
|
+
* step. The committed change drives DocManager's local-update push.
|
|
3283
|
+
*/
|
|
3284
|
+
get editor() {
|
|
3285
|
+
if (this.editorValue == null) throw new Error("mengine doc session is not started");
|
|
3286
|
+
return this.editorValue;
|
|
3287
|
+
}
|
|
3288
|
+
/** Current document snapshot (read model). */
|
|
3289
|
+
snapshot() {
|
|
3290
|
+
if (this.adapterValue == null) throw new Error("mengine doc session is not started");
|
|
3291
|
+
return this.adapterValue.snapshot();
|
|
3292
|
+
}
|
|
3293
|
+
/**
|
|
3294
|
+
* Start the sync stack and connect the document.
|
|
3295
|
+
*
|
|
3296
|
+
* Returns once the local snapshot has loaded into the Loro doc so callers can
|
|
3297
|
+
* read an initial snapshot. Remote convergence continues in the background and
|
|
3298
|
+
* surfaces through `subscribe`.
|
|
3299
|
+
*/
|
|
3300
|
+
async start() {
|
|
3301
|
+
if (this.started) return this.snapshot();
|
|
3302
|
+
this.started = true;
|
|
3303
|
+
this.local.connection.connect();
|
|
3304
|
+
this.server.connection.connect();
|
|
3305
|
+
this.disposables.add(() => this.local.connection.disconnect());
|
|
3306
|
+
this.disposables.add(() => this.server.connection.disconnect());
|
|
3307
|
+
this.synchronizer.start();
|
|
3308
|
+
this.manager.start();
|
|
3309
|
+
this.disposables.add(() => this.synchronizer.stop());
|
|
3310
|
+
this.disposables.add(() => this.manager.stop());
|
|
3311
|
+
const doc = this.manager.connectDoc(this.docId);
|
|
3312
|
+
if (this.options.peerId != null) doc.setPeerId(this.options.peerId);
|
|
3313
|
+
this.adapterValue = new MirrorVideoDocumentAdapter(doc);
|
|
3314
|
+
this.editorValue = new SemanticEditor(this.adapterValue);
|
|
3315
|
+
this.disposables.add(() => this.manager.disconnectDoc(this.docId));
|
|
3316
|
+
const unsubscribe = doc.subscribe((event) => {
|
|
3317
|
+
if (this.adapterValue == null) return;
|
|
3318
|
+
this.events.emit("update", {
|
|
3319
|
+
source: event.by === "local" ? "local" : "remote",
|
|
3320
|
+
snapshot: this.adapterValue.snapshot()
|
|
3321
|
+
});
|
|
3322
|
+
});
|
|
3323
|
+
this.disposables.add(unsubscribe);
|
|
3324
|
+
this.disposables.add(this.manager.onDocStateChange(this.docId, (state) => this.events.emit("state", state)));
|
|
3325
|
+
await this.waitForContent();
|
|
3326
|
+
return this.snapshot();
|
|
3327
|
+
}
|
|
3328
|
+
subscribe(cb) {
|
|
3329
|
+
return this.events.on("update", cb);
|
|
3330
|
+
}
|
|
3331
|
+
onStateChange(cb) {
|
|
3332
|
+
return this.events.on("state", cb);
|
|
3333
|
+
}
|
|
3334
|
+
getState() {
|
|
3335
|
+
return this.manager.getDocState(this.docId);
|
|
3336
|
+
}
|
|
3337
|
+
destroy() {
|
|
3338
|
+
this.disposables.dispose();
|
|
3339
|
+
this.events.clearAll();
|
|
3340
|
+
this.adapterValue = null;
|
|
3341
|
+
this.editorValue = null;
|
|
3342
|
+
this.started = false;
|
|
3343
|
+
}
|
|
3344
|
+
/**
|
|
3345
|
+
* Resolve once the Loro doc holds the document root.
|
|
3346
|
+
*
|
|
3347
|
+
* `DocManager` loads from local storage, which starts empty for a fresh
|
|
3348
|
+
* client; the server snapshot arrives asynchronously via the first sync job.
|
|
3349
|
+
* `loaded` only means the local load ran, so we wait for actual content
|
|
3350
|
+
* (populated schema roots) instead, surfaced by the doc subscription set up
|
|
3351
|
+
* in `start()`.
|
|
3352
|
+
*/
|
|
3353
|
+
async waitForContent() {
|
|
3354
|
+
if (this.hasContent()) return;
|
|
3355
|
+
await new Promise((resolve) => {
|
|
3356
|
+
const off = this.events.on("update", () => {
|
|
3357
|
+
if (this.hasContent()) {
|
|
3358
|
+
off();
|
|
3359
|
+
resolve();
|
|
3360
|
+
}
|
|
3361
|
+
});
|
|
3362
|
+
});
|
|
3363
|
+
}
|
|
3364
|
+
hasContent() {
|
|
3365
|
+
if (this.adapterValue == null) return false;
|
|
3366
|
+
try {
|
|
3367
|
+
return this.adapterValue.hasContent();
|
|
3368
|
+
} catch {
|
|
3369
|
+
return false;
|
|
3370
|
+
}
|
|
3371
|
+
}
|
|
3372
|
+
};
|
|
3373
|
+
//#endregion
|
|
3374
|
+
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 };
|