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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
- import { t as __exportAll } from "./chunk-D7D4PA-g.js";
2
- import { A as partDurationMs, C as reassignSpeechesToVideoClipsByTime, D as syncAggregatedClipsTimePosition, E as resolveSpeechOverlapByShiftingVideos, F as partUnionToDraft, I as recordEntries, L as videoDocumentMirrorSchema, M as DEFAULT_UNIT_TIME_MS, N as VIDEO_DOCUMENT_SCHEMA_VERSION, O as TIMELINE_SKELETON_DURATION_MS, P as effectiveVideoClipDurationMs, R as base64ToBytes, S as fillMainTrackTimeGaps, T as resolveAllSpeechOverlaps, _ as findLaneTrack, a as buildInitialVideoDocument, b as cascadeAfterVideoClipChanges, c as fromVideoDocument, d as assertValidVideoDocument, f as validateVideoDocument, g as ensureLaneTrack, h as LANE_KINDS_IN_STACK_ORDER, i as readVideoDocumentFromDraft, j as safeDurationMs, k as isEmptyVideoClip, l as toVideoDocument, m as videoDocumentSchema, n as createMirrorVideoDocument, o as buildSpeechHostMap, p as partUnionSchema, r as createMirrorVideoDocumentAdapter, s as derivePositionFromAbs, t as MirrorVideoDocumentAdapter, u as VideoDocumentValidationError, v as laneTrackId, w as recalculateTimelineDuration, x as arrangeMainTrackSeamlessly, y as solveVideoDocument, z as bytesToBase64 } from "./document-Dcu87api.js";
3
- import { z } from "zod";
1
+ import { A as resolveAllSpeechOverlaps, B as effectiveVideoClipDurationMs, C as solveVideoDocument, D as reassignSpeechesToVideoClipsByTime, E as fillMainTrackTimeGaps, F as isEmptyVideoClip, G as base64ToBytes, H as partUnionToDraft, I as partDurationMs, K as bytesToBase64, L as safeDurationMs, M as resolveSpeechOverlapByShiftingVideos, N as syncAggregatedClipsTimePosition, O as recalculateTimelineDuration, P as TIMELINE_SKELETON_DURATION_MS, R as DEFAULT_UNIT_TIME_MS, S as laneTrackId, T as arrangeMainTrackSeamlessly, U as recordEntries, V as speedOf, W as videoDocumentMirrorSchema, _ as partUnionSchema, a as createMirrorVideoDocument, b as ensureLaneTrack, c as readVideoDocumentFromDraft, d as derivePositionFromAbs, f as fromVideoDocument, g as validateVideoDocument, h as assertValidVideoDocument, i as MirrorVideoDocumentAdapter, j as resolveAnchoredClipsTimePosition, k as recoverOrphanedSpeechAnchorsByTime, l as buildInitialVideoDocument, m as VideoDocumentValidationError, n as createPlainMemoryAdapter, o as createMirrorVideoDocumentAdapter, p as toVideoDocument, r as generatePartId, s as writeVideoDocumentToDraft, t as PlainMemoryAdapter, u as buildSpeechHostMap, v as videoDocumentSchema, w as cascadeAfterVideoClipChanges, x as findLaneTrack, y as LANE_KINDS_IN_STACK_ORDER, z as VIDEO_DOCUMENT_SCHEMA_VERSION } from "./document-vV4IbKWB.js";
2
+ import { addSpeechesInputSchema, addVideoClipsInputSchema, adjustBgmVolumeInputSchema, adjustSpeechVolumeInputSchema, adjustVideoClipDurationInputSchema, adjustVideoClipVolumeInputSchema, changeSpeechScriptInputSchema, changeSpeechVoiceInputSchema, deleteBgmInputSchema, deleteSpeechesInputSchema, deleteVideoClipsInputSchema, moveBgmInputSchema, moveSpeechesInputSchema, moveVideoClipsByAnchorInputSchema, moveVideoClipsInputSchema, replaceVideoClipContentInputSchema, replaceVideoClipSequenceInputSchema, setBgmInputSchema, setCaptionStyleInputSchema, setCaptionVisibilityInputSchema, setVideoClipSpeedShiftInputSchema, t as schemas_exports } from "./schemas.js";
4
3
  import { LoroDoc, VersionVector } from "loro-crdt";
5
4
  import { ClientServerSynchronizer, DocManager } from "@mengine/sync";
6
5
  import { DisposableSet, EventBus, Task } from "@mengine/utils";
@@ -218,574 +217,6 @@ function findSseFrameBoundary(buffer) {
218
217
  return Math.min(lf, crlf);
219
218
  }
220
219
  //#endregion
221
- //#region src/editor/id-gen.ts
222
- /**
223
- * Part-id generation, aligned with the online ecosystem.
224
- *
225
- * The authoritative online producers — agent-harness (`@harness/shared`
226
- * `genObjId`) and director.v2 (`common/obj_id.py` `gen_obj_id`) — both mint part
227
- * ids as `` `${prefix}_${ulid()}` ``, and real captured drafts use exactly that
228
- * shape (`clip_…` / `spe_…` / `cap_…` / `bgm_…`, each a 26-char ULID). The engine
229
- * previously emitted `vc_<base36 timestamp><6 random>`, a different prefix AND a
230
- * different encoding — the sole cross-repo id divergence. This module removes it
231
- * by emitting the same `<prefix>_<ULID>` bytes.
232
- *
233
- * The ULID is generated inline (Crockford Base32, 48-bit time + 80-bit random)
234
- * rather than pulling the `ulid` npm package: the randomness class matches the
235
- * old generator (both `Math.random`-based) and it keeps `@mengine/medeo-client`
236
- * dependency-free for a purely mechanical id string. Part ids only need to be
237
- * unique and lexicographically time-sortable, which this satisfies.
238
- */
239
- /** Crockford Base32 alphabet (no I, L, O, U), per the ULID spec. */
240
- const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
241
- const TIME_LEN = 10;
242
- const RANDOM_LEN = 16;
243
- function encodeTime(now) {
244
- let out = "";
245
- let ms = now;
246
- for (let i = TIME_LEN - 1; i >= 0; i--) {
247
- const mod = ms % 32;
248
- out = CROCKFORD[mod] + out;
249
- ms = (ms - mod) / 32;
250
- }
251
- return out;
252
- }
253
- function encodeRandom() {
254
- let out = "";
255
- for (let i = 0; i < RANDOM_LEN; i++) out += CROCKFORD[Math.floor(Math.random() * 32)];
256
- return out;
257
- }
258
- /** A 26-char Crockford Base32 ULID (10-char time + 16-char random). */
259
- function ulid() {
260
- return encodeTime(Date.now()) + encodeRandom();
261
- }
262
- function generatePartId(prefix) {
263
- return `${prefix}_${ulid()}`;
264
- }
265
- //#endregion
266
- //#region src/editor/schemas/shared.ts
267
- const clipIdSchema = z.string().min(1).describe("The clip part ID on the timeline");
268
- 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)");
269
- const mediaIdSchema = z.string().min(1).describe("The media asset ID");
270
- const speechIdSchema = z.string().min(1).describe("The speech part ID on the timeline");
271
- const timelineMsSchema = z.number().int().min(0).describe("Time position in milliseconds on the timeline (>= 0)");
272
- const positiveMsSchema = z.number().int().positive().describe("Duration in milliseconds (> 0)");
273
- const volumeSchema = z.number().min(-60).max(20).describe("Volume in decibels (-60.0 to 20.0; 0.0 = original, -60 = mute, +20 = max)");
274
- 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)");
275
- const tangentHandleSchema = z.object({
276
- x: z.number().finite(),
277
- y: z.number().finite()
278
- }).describe("Bezier tangent handle (x, y)");
279
- const speedKeyframeSchema = z.object({
280
- position: z.number().min(0).max(1),
281
- rate: z.number().min(0),
282
- in_tangent: tangentHandleSchema.optional(),
283
- out_tangent: tangentHandleSchema.optional()
284
- }).describe("A speed keyframe: normalized position (0..1), rate, optional tangents");
285
- /**
286
- * A clip's playback-speed fact, the only thing `SetVideoClipSpeedShift` writes.
287
- * Mirrors the IDL `SpeedShift`: `category` is `linear` | `curve`, and `config`
288
- * is a discriminated union — `{ linear: { speed } }` for a constant multiplier
289
- * (the multiplier projection reads at `config.linear.speed`) or `{ curve: {
290
- * keyframes } }` for a Bezier-controlled variable speed (RFC 02 / `reference/16`
291
- * §0). Exactly one of `linear` / `curve` is present.
292
- */
293
- const speedShiftSchema = z.object({
294
- category: z.enum(["linear", "curve"]),
295
- mode: z.string(),
296
- config: z.union([z.object({ linear: z.object({ speed: z.number().finite().positive() }) }), z.object({ curve: z.object({ keyframes: z.array(speedKeyframeSchema).min(2) }) })])
297
- }).describe("Speed shift: linear multiplier or Bezier curve, mirroring the IDL shape");
298
- const voiceSchema = z.object({
299
- id: z.string().min(1),
300
- name: z.string()
301
- }).describe("TTS voice summary attached to a speech");
302
- //#endregion
303
- //#region src/editor/schemas/speech-assets.ts
304
- /**
305
- * The materialized TTS result shared by `AddSpeeches` / `ChangeSpeechScript` /
306
- * `ChangeSpeechVoice` (see `results/phase-4-side-effect-payload-contract.md`
307
- * §1/§2). The side effect (TTS/ASR + billing) runs upstream; the op receives the
308
- * stable speech + caption parts and writes them as authoritative facts. No
309
- * cascade runs on write — the projection derives absolute positions on read.
310
- *
311
- * Each speech carries the anchoring fact directly (RFC 02 §4): the host video
312
- * clip `anchor_part_id` and the `offset_ms` within it. The upstream caller
313
- * already knows which clip a speech attaches to, so the op writes
314
- * `{ mode:'anchored', anchorPartId, offsetMs }` verbatim — no write-time
315
- * host-picking. Captions anchor to their speech via the caption part's
316
- * `start_ms` (offset within the speech).
317
- */
318
- const speechAssetSchema = z.object({
319
- speech_id: speechIdSchema.describe("The speech part ID (= side-effect speech_parts[].id)"),
320
- anchor_part_id: clipIdSchema.describe("Host video clip part ID the speech anchors to (RFC 02 §4)"),
321
- offset_ms: timelineMsSchema.describe("Offset within the host clip (speech.abs = host.abs + offset_ms)"),
322
- audio_storage_key: z.string().min(1),
323
- duration_ms: positiveMsSchema,
324
- audio_script: z.string(),
325
- volume: volumeSchema,
326
- voice: voiceSchema,
327
- origin_speech_id: z.string().min(1),
328
- caption_ids: z.array(z.string().min(1)).describe("Caption part IDs owned by this speech")
329
- });
330
- const captionAssetSchema = z.object({
331
- caption_id: z.string().min(1).describe("The caption part ID (= side-effect created_caption_parts[].id)"),
332
- speech_part_id: speechIdSchema.describe("The owning speech part ID"),
333
- text: z.string(),
334
- start_ms: timelineMsSchema.describe("Offset within the host speech (caption.abs = speech.abs + start_ms)"),
335
- duration_ms: positiveMsSchema
336
- });
337
- /** A materialized speech-subtree write (speeches + their captions). */
338
- const speechAssetsSchema = z.object({
339
- speeches: z.array(speechAssetSchema).min(1).describe("Materialized speech parts to write"),
340
- captions: z.array(captionAssetSchema).describe("Materialized caption parts owned by the speeches")
341
- });
342
- //#endregion
343
- //#region src/editor/schemas/add-speeches.ts
344
- /**
345
- * Add speeches (and their captions). TTS runs upstream; the stable speech /
346
- * caption parts arrive materialized (see `speech-assets.ts`). The op writes the
347
- * parts and each speech's `{ mode:'anchored', anchorPartId, offsetMs }` fact
348
- * verbatim — no write-time host-picking, no cascade (RFC 02 §4). The projection
349
- * derives absolute positions on read.
350
- */
351
- const addSpeechesInputSchema = speechAssetsSchema.describe("Materialized speeches + captions to add");
352
- //#endregion
353
- //#region src/editor/schemas/add-video-clips.ts
354
- /**
355
- * Add video clips to a track. Each clip's duration facts are separated so a
356
- * single number is never overloaded (RFC 02 / `reference/16` §0b):
357
- *
358
- * - `media_duration_ms` is the source media's intrinsic full length (a resource
359
- * fact, written to the part);
360
- * - `play_in` / `play_out` are the optional trim window into that media; when
361
- * omitted the whole media is used (`play_in=0`, `play_out=media_duration_ms`).
362
- *
363
- * The clip's effective timeline duration is derived by the projection from the
364
- * trim window and `speed_shift` — it is never an input here.
365
- */
366
- const addVideoClipsInputSchema = z.object({
367
- clips: z.array(z.object({
368
- media_id: mediaIdSchema.describe("The media asset ID for the video clip"),
369
- start_ms: timelineMsSchema.optional().describe("Absolute start time in milliseconds on the timeline"),
370
- media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
371
- play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
372
- play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)"),
373
- track_id: z.string().min(1).optional().describe("Target track ID (optional, defaults to main track)")
374
- })).min(1).describe("List of video clips to create"),
375
- before_clip_id: z.string().min(1).optional().describe("Insert new clips before this clip ID"),
376
- after_clip_id: z.string().min(1).optional().describe("Insert new clips after this clip ID")
377
- }).superRefine((data, ctx) => {
378
- if (data.before_clip_id != null && data.after_clip_id != null) {
379
- ctx.addIssue({
380
- code: z.ZodIssueCode.custom,
381
- message: "Cannot provide both before_clip_id and after_clip_id"
382
- });
383
- return;
384
- }
385
- const hasRelative = data.before_clip_id != null || data.after_clip_id != null;
386
- for (let i = 0; i < data.clips.length; i++) {
387
- const clip = data.clips[i];
388
- if (hasRelative && clip.start_ms != null) ctx.addIssue({
389
- code: z.ZodIssueCode.custom,
390
- message: `clips[${i}].start_ms must not be provided when using before_clip_id or after_clip_id`,
391
- path: [
392
- "clips",
393
- i,
394
- "start_ms"
395
- ]
396
- });
397
- if (!hasRelative && clip.start_ms == null) ctx.addIssue({
398
- code: z.ZodIssueCode.custom,
399
- message: `clips[${i}].start_ms is required when not using relative positioning`,
400
- path: [
401
- "clips",
402
- i,
403
- "start_ms"
404
- ]
405
- });
406
- const playIn = clip.play_in ?? 0;
407
- const playOut = clip.play_out ?? clip.media_duration_ms;
408
- if (playOut > clip.media_duration_ms) ctx.addIssue({
409
- code: z.ZodIssueCode.custom,
410
- message: `clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
411
- path: [
412
- "clips",
413
- i,
414
- "play_out"
415
- ]
416
- });
417
- if (playIn >= playOut) ctx.addIssue({
418
- code: z.ZodIssueCode.custom,
419
- message: `clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
420
- path: [
421
- "clips",
422
- i,
423
- "play_in"
424
- ]
425
- });
426
- }
427
- });
428
- //#endregion
429
- //#region src/editor/schemas/adjust-bgm-volume.ts
430
- const adjustBgmVolumeInputSchema = z.object({ bgm: z.array(z.object({
431
- bgm_id: clipIdSchema.describe("The bgm part ID to adjust volume for"),
432
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
433
- })).min(1).describe("List of bgm parts with their new volume settings") });
434
- //#endregion
435
- //#region src/editor/schemas/adjust-speech-volume.ts
436
- const adjustSpeechVolumeInputSchema = z.object({ speeches: z.array(z.object({
437
- speech_id: speechIdSchema.describe("The speech part ID to adjust volume for"),
438
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
439
- })).min(1).describe("List of speeches with their new volume settings") });
440
- //#endregion
441
- //#region src/editor/schemas/adjust-video-clip-duration.ts
442
- /**
443
- * Re-trim existing video clips (the user-facing "adjust duration" gesture is a
444
- * trim of the source window). The new `play_in` / `play_out` are the facts; the
445
- * effective timeline duration is derived from them and the clip's `speed_shift`,
446
- * and the change reflows downstream clips, speeches, and the timeline inside the
447
- * op's transaction (no caller-materialized cascade).
448
- */
449
- const adjustVideoClipDurationInputSchema = z.object({ clips: z.array(z.object({
450
- clip_id: clipIdSchema.describe("The video clip part ID to re-trim"),
451
- play_in: timelineMsSchema.describe("New trim window start in the source media"),
452
- play_out: positiveMsSchema.describe("New trim window end in the source media")
453
- })).min(1).describe("Video clips with their new trim windows") });
454
- //#endregion
455
- //#region src/editor/schemas/adjust-video-clip-volume.ts
456
- const adjustVideoClipVolumeInputSchema = z.object({ clips: z.array(z.object({
457
- clip_id: clipIdSchema.describe("The video clip part ID to adjust volume for"),
458
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
459
- })).min(1).describe("List of video clips with their new volume settings") });
460
- //#endregion
461
- //#region src/editor/schemas/change-speech.ts
462
- /**
463
- * Change a speech's script or voice. Both re-run TTS upstream and return the
464
- * regenerated speech / caption parts in the same materialized shape as
465
- * `AddSpeeches` (`speech-assets.ts`); the op upserts them by id (the speech part
466
- * id is preserved across a re-TTS), re-seats at `start_ms`, and reflows. Old
467
- * caption parts no longer owned by the speech are removed via `caption_ids`.
468
- */
469
- const changeSpeechScriptInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new script)");
470
- const changeSpeechVoiceInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new voice)");
471
- //#endregion
472
- //#region src/editor/schemas/delete-bgm.ts
473
- /**
474
- * Remove the document BGM. Pure document edit: clears the bgm lane and removes
475
- * the bgm part. Takes no input (a document holds at most one bgm); an empty
476
- * object keeps the op signature uniform with the rest.
477
- */
478
- const deleteBgmInputSchema = z.object({}).describe("Remove the document BGM (no parameters)");
479
- //#endregion
480
- //#region src/editor/schemas/delete-speeches.ts
481
- /**
482
- * Delete speeches with their captions. Pure document edit (no side effect): the
483
- * op removes each speech part, cascade-deletes the captions it owns (via
484
- * `caption_ids` / `speech_part_id`), drops their track items, and reflows.
485
- */
486
- const deleteSpeechesInputSchema = z.object({ speech_ids: speechIdsSchema.describe("Speech part IDs to delete (their captions cascade-delete)") });
487
- //#endregion
488
- //#region src/editor/schemas/delete-video-clips.ts
489
- /**
490
- * How a delete handles the anchored subtree (speeches anchored to a deleted clip,
491
- * and their captions) — a delete-op policy, not a data-model field (reference/17
492
- * §6). `cascade` (default) removes the subtree; `detach` keeps the direct
493
- * anchored children, re-pinning them to `absolute` so they stay on the timeline.
494
- */
495
- const anchoredDeletePolicySchema = z.enum(["cascade", "detach"]);
496
- const deleteVideoClipsInputSchema = z.object({
497
- clip_ids: clipIdsSchema.describe("List of video clip part IDs to delete from the main track"),
498
- on_anchored: anchoredDeletePolicySchema.optional().describe("How to treat anchored children (default cascade)")
499
- });
500
- //#endregion
501
- //#region src/editor/schemas/move-speeches.ts
502
- /**
503
- * Move speeches in time. Pure document edit: the op re-seats each speech at its
504
- * new absolute `start_ms`; the cascade reassigns it to the host video clip,
505
- * resolves overlaps, and reflows. Captions follow their speech.
506
- */
507
- const moveSpeechesInputSchema = z.object({ speeches: z.array(z.object({
508
- speech_id: speechIdSchema.describe("The speech part ID to move"),
509
- new_start_ms: timelineMsSchema.describe("New absolute start time on the timeline")
510
- })).min(1).describe("Speeches to move to new positions") });
511
- //#endregion
512
- //#region src/editor/schemas/move-video-clips-by-anchor.ts
513
- /**
514
- * Where the moved block lands on the main track.
515
- *
516
- * A discriminated union rather than two optional `before_clip_id` /
517
- * `after_clip_id` fields (the shape `addVideoClips` had to use, because there the
518
- * two modes share a whole clip description): here the alternatives carry nothing
519
- * in common, so making them mutually exclusive *by type* removes three runtime
520
- * `superRefine` checks that would otherwise have to be written and tested.
521
- *
522
- * `track_start` is an explicit member, not the absence of an anchor. The
523
- * agent-harness mutation this maps from treats "neither anchor given" as
524
- * "move to the front" (`applyBatchMoveVideoClips` falls back to `insertIndex = 0`),
525
- * which is a default buried in a tool description. Requiring the caller to name
526
- * that intent keeps a forgotten field from silently reordering the timeline.
527
- */
528
- const moveAnchorSchema = z.discriminatedUnion("position", [
529
- z.object({
530
- position: z.literal("before"),
531
- clip_id: clipIdSchema.describe("The moved block lands immediately before this clip")
532
- }),
533
- z.object({
534
- position: z.literal("after"),
535
- clip_id: clipIdSchema.describe("The moved block lands immediately after this clip")
536
- }),
537
- z.object({ position: z.literal("track_start") })
538
- ]).describe("Where the moved block lands: before/after a reference clip, or at the head of the track");
539
- /**
540
- * What happens to the speeches anchored to the clips being moved.
541
- *
542
- * - `follow` keeps each speech anchored where it is, so it travels with its clip
543
- * to the new position. In the anchored model this is the *no-op* branch: a
544
- * speech's authoritative fact is `{ anchorPartId, offsetMs }` and its absolute
545
- * time is derived on read from the host's position, so moving the host moves
546
- * the speech with no write to the speech at all.
547
- * - `keep_absolute` preserves each speech's current absolute landing instead, then
548
- * re-anchors it to whichever clip now covers that time (RFC 02 §9.1/§11.1). This
549
- * is the branch that costs an extra pass, and the one the FE timeline uses.
550
- *
551
- * **Required, with no default**, matching `deleteVideoClips` and
552
- * `replaceVideoClipSequence`. The two branches decide which picture the user's
553
- * narration ends up over, which is too consequential to infer from a missing
554
- * field — and neither branch is "safe enough" to be the implicit one.
555
- */
556
- const movedClipAnchoredPolicySchema = z.enum(["follow", "keep_absolute"]);
557
- /**
558
- * Reorder a set of main-track clips relative to a reference clip.
559
- *
560
- * Distinct from `moveVideoClips`, which positions clips by absolute time
561
- * (`new_start_ms`) and is what the FE timeline dispatches after a drag. Main-track
562
- * clips are `sequential`-positioned, so absolute time is not authoritative state
563
- * there (RFC 02 §4/§7): `moveVideoClips` has to *guess* an index back out of the
564
- * time it was handed, whereas an anchor already is the ordinal fact being changed.
565
- * Keeping them separate also isolates blast radius — this method can diverge on
566
- * speech policy without touching the FE path.
567
- *
568
- * `clip_ids` need not be contiguous. They move as one block, keeping their
569
- * relative order, which is the agent-harness `batch_move_video_clips` contract.
570
- */
571
- const moveVideoClipsByAnchorInputSchema = z.object({
572
- clip_ids: clipIdsSchema.describe("Clips to move as one block, keeping their relative order. Need not be contiguous on the track."),
573
- anchor: moveAnchorSchema,
574
- on_anchored: movedClipAnchoredPolicySchema.describe("What happens to speeches anchored to the moved clips (required — see the policy doc)")
575
- });
576
- //#endregion
577
- //#region src/editor/schemas/move-video-clips.ts
578
- const moveVideoClipsInputSchema = z.object({ clips: z.array(z.object({
579
- clip_id: clipIdSchema.describe("The video clip part ID to move"),
580
- new_start_ms: timelineMsSchema.describe("New absolute start time in milliseconds on the timeline"),
581
- new_track_id: z.string().min(1).optional().describe("Target track ID to move the clip to (optional)")
582
- })).min(1).describe("List of video clips to move to new positions") });
583
- //#endregion
584
- //#region src/editor/schemas/replace-video-clip-content.ts
585
- /**
586
- * Replace the media backing existing video clips. The media import runs upstream
587
- * (Director); its stable result — the new media id, intrinsic length, and the
588
- * reset trim window — arrives materialized (see
589
- * `results/phase-4-side-effect-payload-contract.md` §4). Director resets
590
- * `play_in=0` / `play_out=media_duration_ms` and clears `speed_shift` on
591
- * replacement. The clip `part_id`s (hence their track items) are unchanged; the
592
- * editor reflows the main track from the new effective durations.
593
- */
594
- const replaceVideoClipContentInputSchema = z.object({ clips: z.array(z.object({
595
- clip_id: clipIdSchema.describe("Existing video clip part ID to re-point"),
596
- origin_media_id: mediaIdSchema.describe("The new media asset ID"),
597
- media_duration_ms: positiveMsSchema.describe("The new media's intrinsic full length"),
598
- play_in: timelineMsSchema.describe("Trim window start in the new media (usually 0)"),
599
- play_out: positiveMsSchema.describe("Trim window end in the new media (usually = media_duration_ms)"),
600
- volume: volumeSchema
601
- })).min(1).describe("Video clips whose media is being replaced") });
602
- //#endregion
603
- //#region src/editor/schemas/replace-video-clip-sequence.ts
604
- /**
605
- * What happens to the speeches anchored to the clips being replaced.
606
- *
607
- * - `remap` re-anchors each surviving speech to the new clip in the SAME POSITION
608
- * of the sequence, keeping its offset — old[i]'s children become new[i]'s
609
- * children. An old clip with no counterpart (fewer new clips than old) has its
610
- * subtree deleted, because there is nothing left to anchor to.
611
- * - `cascade` deletes every anchored speech (and its captions) outright, like
612
- * `deleteVideoClips`.
613
- *
614
- * **Required, with no default.** The two branches differ in whether the user's
615
- * narration survives, and the agent tool that drives this op makes its
616
- * `preserve_speeches` flag required for that reason. A default here would let a
617
- * caller that forgot the field silently delete speech.
618
- */
619
- const anchoredReplacePolicySchema = z.enum(["remap", "cascade"]);
620
- /**
621
- * Replace a contiguous run of main-track clips with a new run.
622
- *
623
- * A composite of delete + insert that cannot be expressed as the two ops in
624
- * sequence, because the anchored speeches have to survive *across* the swap: with
625
- * `remap` they are re-anchored positionally, which needs both the old and the new
626
- * ids in the same transaction (ADR 0009 — the cascade stays in the editor, callers
627
- * never re-wire anchors themselves).
628
- *
629
- * Duration facts follow `addVideoClips`: `media_duration_ms` is the source's
630
- * intrinsic length and the trim window defaults to the whole media. The effective
631
- * timeline duration is derived by the projection, never an input.
632
- *
633
- * `media_id` is optional: omitting it creates a **deliberate empty placeholder
634
- * clip** (`origin_media_id: ''`) — structure with no picture. This is the only op
635
- * that can produce one, and it is authoritative state, unlike the gap fillers the
636
- * read-side solve mints (which never enter the document).
637
- */
638
- const replaceVideoClipSequenceInputSchema = z.object({
639
- old_clip_ids: clipIdsSchema.describe("The clips being replaced: a contiguous main-track run, listed in timeline order"),
640
- new_clips: z.array(z.object({
641
- media_id: mediaIdSchema.optional().describe("The replacement media asset ID. Omit to create an empty placeholder clip."),
642
- media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
643
- play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
644
- play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)")
645
- })).min(1).describe("The replacement clips, in the order they take on the track"),
646
- on_anchored: anchoredReplacePolicySchema.describe("What happens to speeches anchored to the replaced clips (required — see the policy doc)")
647
- }).superRefine((data, ctx) => {
648
- for (let i = 0; i < data.new_clips.length; i++) {
649
- const clip = data.new_clips[i];
650
- const playIn = clip.play_in ?? 0;
651
- const playOut = clip.play_out ?? clip.media_duration_ms;
652
- if (playOut > clip.media_duration_ms) ctx.addIssue({
653
- code: z.ZodIssueCode.custom,
654
- message: `new_clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
655
- path: [
656
- "new_clips",
657
- i,
658
- "play_out"
659
- ]
660
- });
661
- if (playIn >= playOut) ctx.addIssue({
662
- code: z.ZodIssueCode.custom,
663
- message: `new_clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
664
- path: [
665
- "new_clips",
666
- i,
667
- "play_in"
668
- ]
669
- });
670
- }
671
- });
672
- //#endregion
673
- //#region src/editor/schemas/resize-caption.ts
674
- /** Resize one caption placement without changing its cue/source duration. */
675
- const resizeCaptionInputSchema = z.object({
676
- caption_id: z.string().min(1),
677
- duration_ms: z.number().finite().positive()
678
- });
679
- //#endregion
680
- //#region src/editor/schemas/set-bgm.ts
681
- /**
682
- * Set the document BGM. The media's stable result (storage key) arrives
683
- * materialized from upstream (see
684
- * `results/phase-4-side-effect-payload-contract.md` §3). The op upserts the bgm
685
- * part and seats it on the bgm lane; its effective length is always the whole
686
- * timeline, derived by the projection on read — so there is no `duration_ms`
687
- * input or fact (RFC 02 / `reference/16` §0b). A `bgm_id` lets the op replace an
688
- * existing bgm part by id.
689
- */
690
- const setBgmInputSchema = z.object({
691
- bgm_id: z.string().min(1).describe("The bgm part ID to write"),
692
- audio_storage_key: z.string().min(1),
693
- origin_media_id: mediaIdSchema,
694
- volume: volumeSchema
695
- });
696
- //#endregion
697
- //#region src/editor/schemas/set-caption-style.ts
698
- /**
699
- * Set the caption visual style. GLOBAL by design: the style applies to every
700
- * caption part in the document — it carries NO `caption_id`. This mirrors the FE,
701
- * whose caption-style store (`caption-style.ts:persistCaptionStylePatch`) iterates
702
- * ALL captions and writes the same normalized style to each; the product has a
703
- * single document-wide caption style, not per-caption styling.
704
- *
705
- * Every field is optional and maps to a `CaptionStyle` attribute (snake_case
706
- * IDL). A field present in the input is written to every caption; a field ABSENT
707
- * from the input is left untouched on each caption (the editor merges the patch
708
- * onto each caption's existing style — this is a value edit, not a full-style
709
- * replace, so a partial patch such as "recolor only" does not wipe font size).
710
- *
711
- * Pure document edit, no cascade — captions keep their positions; only the style
712
- * sub-map of each caption part changes.
713
- */
714
- const setCaptionStyleInputSchema = z.object({
715
- font_id: z.string().min(1).optional().describe("Font ID referencing a font from the font library"),
716
- font_size: z.number().positive().optional().describe("Font size in points"),
717
- font_color: z.string().min(1).optional().describe("Font color as hex string, e.g. \"#FFFFFF\""),
718
- font_weight: z.number().int().optional().describe("Numeric font weight, e.g. 400 or 700"),
719
- entrance_animation: z.string().optional().describe("Entrance animation preset ID, e.g. \"fade\" or \"none\""),
720
- entrance_animation_duration_ms: z.number().min(0).optional().describe("Entrance animation duration in ms"),
721
- stroke_color: z.string().min(1).optional().describe("Outline/stroke color as hex string, e.g. \"#000000\""),
722
- stroke_width: z.number().min(0).optional().describe("Outline/stroke width in pixels"),
723
- position_x: z.number().optional().describe("Caption center X as a fraction (0.0 to 1.0)"),
724
- position_y: z.number().optional().describe("Caption center Y as a fraction (0.0 to 1.0)")
725
- }).describe("Document-wide caption style patch (no caption_id; applies to every caption)");
726
- //#endregion
727
- //#region src/editor/schemas/set-caption-visibility.ts
728
- /**
729
- * Toggle caption visibility (the caption track's `is_hidden` flag). Pure
730
- * document edit, no cascade — captions keep their positions; only the lane's
731
- * hidden flag changes.
732
- */
733
- const setCaptionVisibilityInputSchema = z.object({ is_hidden: z.boolean().describe("Whether the caption track is hidden") });
734
- //#endregion
735
- //#region src/editor/schemas/set-video-clip-speed-shift.ts
736
- /**
737
- * Set the playback speed of existing video clips. Per the speed-shift decision
738
- * (`reference/16` §0): the op writes only the `speed_shift` fact — it does NOT
739
- * store an effective `duration_ms` (projection derives it from the trim window /
740
- * speed) and does NOT scale anchored speeches' relative offsets (offsets stay
741
- * put; the cascade reflows absolute positions). A `null` speed_shift clears the
742
- * speed back to original (1×).
743
- */
744
- const setVideoClipSpeedShiftInputSchema = z.object({ clips: z.array(z.object({
745
- clip_id: clipIdSchema.describe("The video clip part ID to set speed for"),
746
- speed_shift: speedShiftSchema.nullable().describe("The new speed setting, or null to reset to 1×")
747
- })).min(1).describe("Video clips with their new speed settings") });
748
- //#endregion
749
- //#region src/editor/schemas/index.ts
750
- var schemas_exports = /* @__PURE__ */ __exportAll({
751
- addSpeechesInputSchema: () => addSpeechesInputSchema,
752
- addVideoClipsInputSchema: () => addVideoClipsInputSchema,
753
- adjustBgmVolumeInputSchema: () => adjustBgmVolumeInputSchema,
754
- adjustSpeechVolumeInputSchema: () => adjustSpeechVolumeInputSchema,
755
- adjustVideoClipDurationInputSchema: () => adjustVideoClipDurationInputSchema,
756
- adjustVideoClipVolumeInputSchema: () => adjustVideoClipVolumeInputSchema,
757
- anchoredDeletePolicySchema: () => anchoredDeletePolicySchema,
758
- anchoredReplacePolicySchema: () => anchoredReplacePolicySchema,
759
- changeSpeechScriptInputSchema: () => changeSpeechScriptInputSchema,
760
- changeSpeechVoiceInputSchema: () => changeSpeechVoiceInputSchema,
761
- clipIdSchema: () => clipIdSchema,
762
- clipIdsSchema: () => clipIdsSchema,
763
- deleteBgmInputSchema: () => deleteBgmInputSchema,
764
- deleteSpeechesInputSchema: () => deleteSpeechesInputSchema,
765
- deleteVideoClipsInputSchema: () => deleteVideoClipsInputSchema,
766
- mediaIdSchema: () => mediaIdSchema,
767
- moveAnchorSchema: () => moveAnchorSchema,
768
- moveSpeechesInputSchema: () => moveSpeechesInputSchema,
769
- moveVideoClipsByAnchorInputSchema: () => moveVideoClipsByAnchorInputSchema,
770
- moveVideoClipsInputSchema: () => moveVideoClipsInputSchema,
771
- movedClipAnchoredPolicySchema: () => movedClipAnchoredPolicySchema,
772
- positiveMsSchema: () => positiveMsSchema,
773
- replaceVideoClipContentInputSchema: () => replaceVideoClipContentInputSchema,
774
- replaceVideoClipSequenceInputSchema: () => replaceVideoClipSequenceInputSchema,
775
- resizeCaptionInputSchema: () => resizeCaptionInputSchema,
776
- setBgmInputSchema: () => setBgmInputSchema,
777
- setCaptionStyleInputSchema: () => setCaptionStyleInputSchema,
778
- setCaptionVisibilityInputSchema: () => setCaptionVisibilityInputSchema,
779
- setVideoClipSpeedShiftInputSchema: () => setVideoClipSpeedShiftInputSchema,
780
- speechAssetsSchema: () => speechAssetsSchema,
781
- speechIdSchema: () => speechIdSchema,
782
- speechIdsSchema: () => speechIdsSchema,
783
- speedShiftSchema: () => speedShiftSchema,
784
- timelineMsSchema: () => timelineMsSchema,
785
- voiceSchema: () => voiceSchema,
786
- volumeSchema: () => volumeSchema
787
- });
788
- //#endregion
789
220
  //#region src/editor/snapshot-utils.ts
790
221
  function isMap(value) {
791
222
  return value instanceof Map;
@@ -1014,6 +445,10 @@ var SchemaValidator = class {
1014
445
  const snapshot = doc.snapshot();
1015
446
  for (const s of parsed.speeches) this.assertPartKind(snapshot, s.speech_id, "speech", "move_speeches");
1016
447
  }
448
+ validateMoveBgm(input, doc) {
449
+ this.parse(moveBgmInputSchema, input, "move_bgm_invalid_input");
450
+ if (doc.snapshot().tracks?.find((track) => track.parts_kind === "bgm")?.items?.[0] == null) throw new ValidationError("move_bgm_not_found", "document has no BGM item");
451
+ }
1017
452
  validateSetBgm(input, _doc) {
1018
453
  this.parse(setBgmInputSchema, input, "set_bgm_invalid_input");
1019
454
  }
@@ -1023,18 +458,6 @@ var SchemaValidator = class {
1023
458
  validateSetCaptionVisibility(input, _doc) {
1024
459
  this.parse(setCaptionVisibilityInputSchema, input, "set_caption_visibility_invalid_input");
1025
460
  }
1026
- validateResizeCaption(input, doc) {
1027
- const parsed = this.parse(resizeCaptionInputSchema, input, "resize_caption_invalid_input");
1028
- const snapshot = doc.snapshot();
1029
- this.assertPartKind(snapshot, parsed.caption_id, "caption", "resize_caption");
1030
- if (snapshot.tracks?.find((track) => track.parts_kind === "caption")?.items?.find((row) => row.part_id === parsed.caption_id) == null) throw new ValidationError("resize_caption_placement_not_found", `caption_id "${parsed.caption_id}" is not placed on the caption track`, { caption_id: parsed.caption_id });
1031
- const cueDuration = snapshot.part_library?.[parsed.caption_id]?.caption?.initial_duration_ms ?? 0;
1032
- if (parsed.duration_ms < cueDuration) throw new ValidationError("resize_caption_below_cue", `duration_ms ${parsed.duration_ms} is shorter than cue duration ${cueDuration}`, {
1033
- caption_id: parsed.caption_id,
1034
- duration_ms: parsed.duration_ms,
1035
- cue_duration_ms: cueDuration
1036
- });
1037
- }
1038
461
  validateSetCaptionStyle(input, _doc) {
1039
462
  this.parse(setCaptionStyleInputSchema, input, "set_caption_style_invalid_input");
1040
463
  }
@@ -1061,16 +484,20 @@ var SchemaValidator = class {
1061
484
  for (const speech of assets.speeches) this.assertPartKind(snapshot, speech.speech_id, "speech", opName);
1062
485
  }
1063
486
  /**
1064
- * Each speech's `anchor_part_id` must point at a video clip that already
1065
- * exists. A relative speech anchored to a missing clip cannot be positioned
1066
- * by the projection and has no host to recover to (RFC 02 §4/§11.1), so the
1067
- * write must be rejected rather than landing a dangling reference. (Restores
1068
- * the dangling-reference guard the old `validateMaterializedPatch` carried;
1069
- * ADR 0009.)
487
+ * Anchored speeches may follow a video or BGM root. A null anchor is an
488
+ * explicit absolute speech root. Missing and wrong-kind parents are rejected.
1070
489
  */
1071
490
  assertAnchorsExist(assets, doc, opName) {
1072
491
  const snapshot = doc.snapshot();
1073
- for (const speech of assets.speeches) this.assertPartKind(snapshot, speech.anchor_part_id, "video_clip", `${opName}_anchor`);
492
+ for (const speech of assets.speeches) {
493
+ if (speech.anchor_part_id == null) continue;
494
+ const part = readPart(snapshot, speech.anchor_part_id);
495
+ if (part == null) throw new ValidationError(`${opName}_anchor_part_not_found`, `part_library has no parent`, { part_id: speech.anchor_part_id });
496
+ if (part.part_kind !== "video_clip" && part.part_kind !== "bgm") throw new ValidationError(`${opName}_anchor_wrong_part_kind`, `speech parent must be video_clip or bgm`, {
497
+ part_id: speech.anchor_part_id,
498
+ part_kind: part.part_kind
499
+ });
500
+ }
1074
501
  }
1075
502
  assertPartKind(snapshot, partId, kind, opName) {
1076
503
  const part = readPart(snapshot, partId);
@@ -1146,9 +573,11 @@ function readPartLibrary(draft) {
1146
573
  var SemanticEditor = class {
1147
574
  doc;
1148
575
  validator;
1149
- constructor(doc, validator = new SchemaValidator()) {
576
+ idFactory;
577
+ constructor(doc, validator = new SchemaValidator(), idFactory = generatePartId) {
1150
578
  this.doc = doc;
1151
579
  this.validator = validator;
580
+ this.idFactory = idFactory;
1152
581
  }
1153
582
  async moveVideoClips(input, options) {
1154
583
  this.validator.validateMoveVideoClips(input, this.doc);
@@ -1228,7 +657,7 @@ var SemanticEditor = class {
1228
657
  track.items ??= [];
1229
658
  let at = insertIndex;
1230
659
  for (const clip of input.clips) {
1231
- const partId = generatePartId("clip");
660
+ const partId = this.idFactory("clip");
1232
661
  setPart(draft, partId, { video_clip: {
1233
662
  id: partId,
1234
663
  kind: "video_clip",
@@ -1240,8 +669,7 @@ var SemanticEditor = class {
1240
669
  track.items.splice(at, 0, {
1241
670
  part_id: partId,
1242
671
  time_position: { mode: "sequential" },
1243
- fallback_abs_ms: void 0,
1244
- duration_override_ms: void 0
672
+ fallback_abs_ms: void 0
1245
673
  });
1246
674
  at += 1;
1247
675
  }
@@ -1276,7 +704,7 @@ var SemanticEditor = class {
1276
704
  const newItems = [];
1277
705
  const newClipIds = [];
1278
706
  for (const clip of input.new_clips) {
1279
- const partId = generatePartId("clip");
707
+ const partId = this.idFactory("clip");
1280
708
  setPart(draft, partId, { video_clip: {
1281
709
  id: partId,
1282
710
  kind: "video_clip",
@@ -1288,8 +716,7 @@ var SemanticEditor = class {
1288
716
  newItems.push({
1289
717
  part_id: partId,
1290
718
  time_position: { mode: "sequential" },
1291
- fallback_abs_ms: void 0,
1292
- duration_override_ms: void 0
719
+ fallback_abs_ms: void 0
1293
720
  });
1294
721
  newClipIds.push(partId);
1295
722
  }
@@ -1419,8 +846,9 @@ var SemanticEditor = class {
1419
846
  */
1420
847
  async addSpeeches(input, options) {
1421
848
  this.validator.validateAddSpeeches(input, this.doc);
849
+ const absByPartId = solveVideoDocument(this.doc.snapshot()).absByPartId;
1422
850
  this.doc.transact((draft) => {
1423
- writeSpeechAssets(draft, input);
851
+ writeSpeechAssets(draft, input, absByPartId);
1424
852
  }, audit("AddSpeeches", input, options));
1425
853
  }
1426
854
  /** Delete speeches with their captions; the subtree is removed (§9.2). Surviving lanes keep their facts. */
@@ -1438,29 +866,60 @@ var SemanticEditor = class {
1438
866
  */
1439
867
  async moveSpeeches(input, options) {
1440
868
  this.validator.validateMoveSpeeches(input, this.doc);
869
+ const snapshot = this.doc.snapshot();
870
+ const absByPartId = solveVideoDocument(snapshot).absByPartId;
1441
871
  this.doc.transact((draft) => {
1442
- const ranges = mainTrackRanges(draft);
1443
872
  const track = findLaneTrack(draft, "speech");
873
+ const ranges = mainTrackRanges(draft);
1444
874
  for (const move of input.speeches) {
1445
875
  const item = (track?.items ?? []).find((it) => it?.part_id === move.speech_id);
1446
876
  if (item == null) continue;
1447
- item.time_position = relativePositionForAbs(ranges, move.new_start_ms);
877
+ const current = item.time_position;
878
+ if (current?.mode === "anchored") {
879
+ const parentStartMs = absByPartId.get(current.anchorPartId);
880
+ item.time_position = readPart(snapshot, current.anchorPartId)?.part_kind === "video_clip" ? relativePositionForAbs(ranges, move.new_start_ms) : parentStartMs == null ? {
881
+ mode: "absolute",
882
+ offsetMs: move.new_start_ms
883
+ } : {
884
+ mode: "anchored",
885
+ anchorPartId: current.anchorPartId,
886
+ offsetMs: Math.max(0, move.new_start_ms - parentStartMs)
887
+ };
888
+ } else item.time_position = {
889
+ mode: "absolute",
890
+ offsetMs: move.new_start_ms
891
+ };
1448
892
  item.fallback_abs_ms = move.new_start_ms;
1449
893
  }
1450
894
  }, audit("MoveSpeeches", input, options));
1451
895
  }
896
+ /** Move the BGM root; any speech/caption descendants follow through their anchors. */
897
+ async moveBgm(input, options) {
898
+ this.validator.validateMoveBgm(input, this.doc);
899
+ this.doc.transact((draft) => {
900
+ const item = findLaneTrack(draft, "bgm")?.items?.[0];
901
+ if (item == null) return;
902
+ item.time_position = {
903
+ mode: "absolute",
904
+ offsetMs: input.new_start_ms
905
+ };
906
+ item.fallback_abs_ms = void 0;
907
+ }, audit("MoveBgm", input, options));
908
+ }
1452
909
  /** Change a speech's script. Re-TTS runs upstream; the regenerated parts arrive materialized (no cascade). */
1453
910
  async changeSpeechScript(input, options) {
1454
911
  this.validator.validateChangeSpeechScript(input, this.doc);
912
+ const absByPartId = solveVideoDocument(this.doc.snapshot()).absByPartId;
1455
913
  this.doc.transact((draft) => {
1456
- writeSpeechAssets(draft, input);
914
+ writeSpeechAssets(draft, input, absByPartId);
1457
915
  }, audit("ChangeSpeechScript", input, options));
1458
916
  }
1459
917
  /** Change a speech's voice. Re-TTS runs upstream; the regenerated parts arrive materialized (no cascade). */
1460
918
  async changeSpeechVoice(input, options) {
1461
919
  this.validator.validateChangeSpeechVoice(input, this.doc);
920
+ const absByPartId = solveVideoDocument(this.doc.snapshot()).absByPartId;
1462
921
  this.doc.transact((draft) => {
1463
- writeSpeechAssets(draft, input);
922
+ writeSpeechAssets(draft, input, absByPartId);
1464
923
  }, audit("ChangeSpeechVoice", input, options));
1465
924
  }
1466
925
  /**
@@ -1502,8 +961,7 @@ var SemanticEditor = class {
1502
961
  mode: "absolute",
1503
962
  offsetMs: 0
1504
963
  },
1505
- fallback_abs_ms: void 0,
1506
- duration_override_ms: void 0
964
+ fallback_abs_ms: void 0
1507
965
  }];
1508
966
  }, audit("SetBgm", input, options));
1509
967
  }
@@ -1526,15 +984,6 @@ var SemanticEditor = class {
1526
984
  track.is_hidden = input.is_hidden;
1527
985
  }, audit("SetCaptionVisibility", input, options));
1528
986
  }
1529
- /** Resize the caption placement window; cue/source duration stays immutable. */
1530
- async resizeCaption(input, options) {
1531
- this.validator.validateResizeCaption(input, this.doc);
1532
- this.doc.transact((draft) => {
1533
- const item = findLaneTrack(draft, "caption")?.items?.find((row) => row.part_id === input.caption_id);
1534
- if (item == null) return;
1535
- item.duration_override_ms = input.duration_ms;
1536
- }, audit("ResizeCaption", input, options));
1537
- }
1538
987
  /**
1539
988
  * Set the document-wide caption style. GLOBAL (no `caption_id`): the patch is
1540
989
  * merged onto EVERY caption part's `style`, mirroring the FE, which applies a
@@ -1699,7 +1148,7 @@ function normalizeSpeedShift(input) {
1699
1148
  * projection derives it on read. On re-TTS the speech id is preserved, so an
1700
1149
  * existing item is re-positioned rather than duplicated.
1701
1150
  */
1702
- function writeSpeechAssets(draft, assets) {
1151
+ function writeSpeechAssets(draft, assets, absByPartId) {
1703
1152
  const captionById = new Map(assets.captions.map((c) => [c.caption_id, c]));
1704
1153
  const speechTrack = ensureLaneTrack(draft, "speech");
1705
1154
  speechTrack.items ??= [];
@@ -1712,8 +1161,6 @@ function writeSpeechAssets(draft, assets) {
1712
1161
  }
1713
1162
  return captionItems;
1714
1163
  };
1715
- const ranges = mainTrackRanges(draft);
1716
- const hostStartMs = (anchorPartId) => ranges.find((r) => r.partId === anchorPartId)?.startMs;
1717
1164
  for (const speech of assets.speeches) {
1718
1165
  const priorCaptionIds = draft.part_library?.[speech.speech_id]?.speech?.caption_ids ?? [];
1719
1166
  const nextCaptionIds = new Set(speech.caption_ids);
@@ -1737,9 +1184,10 @@ function writeSpeechAssets(draft, assets) {
1737
1184
  voice: speech.voice,
1738
1185
  caption_ids: speech.caption_ids
1739
1186
  } });
1740
- const hostStart = hostStartMs(speech.anchor_part_id);
1741
- const speechAbs = hostStart == null ? void 0 : hostStart + speech.offset_ms;
1742
- placeRelative(speechTrack.items, speech.speech_id, speech.anchor_part_id, speech.offset_ms, speechAbs);
1187
+ const hostStart = speech.anchor_part_id == null ? void 0 : absByPartId.get(speech.anchor_part_id);
1188
+ const speechAbs = speech.anchor_part_id == null ? speech.offset_ms : hostStart == null ? void 0 : hostStart + speech.offset_ms;
1189
+ if (speech.anchor_part_id == null) placeAbsolute(speechTrack.items, speech.speech_id, speech.offset_ms);
1190
+ else placeRelative(speechTrack.items, speech.speech_id, speech.anchor_part_id, speech.offset_ms, speechAbs);
1743
1191
  for (const captionId of speech.caption_ids) {
1744
1192
  const caption = captionById.get(captionId);
1745
1193
  if (caption == null) continue;
@@ -1781,8 +1229,24 @@ function placeRelative(items, partId, anchorPartId, offsetMs, fallbackAbsMs) {
1781
1229
  items.push({
1782
1230
  part_id: partId,
1783
1231
  time_position: timePosition,
1784
- fallback_abs_ms: fallbackAbsMs,
1785
- duration_override_ms: void 0
1232
+ fallback_abs_ms: fallbackAbsMs
1233
+ });
1234
+ }
1235
+ function placeAbsolute(items, partId, startMs) {
1236
+ const timePosition = {
1237
+ mode: "absolute",
1238
+ offsetMs: Math.max(0, Math.round(startMs))
1239
+ };
1240
+ const existing = items.find((item) => item?.part_id === partId);
1241
+ if (existing != null) {
1242
+ existing.time_position = timePosition;
1243
+ existing.fallback_abs_ms = void 0;
1244
+ return;
1245
+ }
1246
+ items.push({
1247
+ part_id: partId,
1248
+ time_position: timePosition,
1249
+ fallback_abs_ms: void 0
1786
1250
  });
1787
1251
  }
1788
1252
  /**
@@ -1908,6 +1372,114 @@ function deleteAnchoredSubtree(draft, clipId) {
1908
1372
  for (const speechId of anchored) deleteSpeechSubtree(draft, speechId);
1909
1373
  }
1910
1374
  //#endregion
1375
+ //#region src/editor/journal.ts
1376
+ /**
1377
+ * Wire a plain-memory adapter + editor that share a recording id factory, so
1378
+ * every mutating transact lands in `journal` with ordered `generated_ids`.
1379
+ */
1380
+ function createEditSandbox(document, options) {
1381
+ const adapter = createPlainMemoryAdapter(document, options);
1382
+ return {
1383
+ adapter,
1384
+ editor: new SemanticEditor(adapter, new SchemaValidator(), adapter.idFactory),
1385
+ journal: adapter.journal
1386
+ };
1387
+ }
1388
+ /**
1389
+ * Re-drive `doc` from a recorded journal, forcing each entry's `generated_ids`
1390
+ * through a queue-backed id factory. Never mints fresh ids: an empty queue on
1391
+ * demand throws `unrecorded id`; leftover ids after an entry throws
1392
+ * `unconsumed ids`. Legacy entries without `generated_ids` are treated as `[]`.
1393
+ */
1394
+ async function replayJournal(doc, journal) {
1395
+ const queue = [];
1396
+ const idFactory = (_prefix) => {
1397
+ const id = queue.shift();
1398
+ if (id == null) throw new Error("unrecorded id");
1399
+ return id;
1400
+ };
1401
+ const editor = new SemanticEditor(doc, new SchemaValidator(), idFactory);
1402
+ for (const entry of journal) {
1403
+ queue.push(...entry.generated_ids ?? []);
1404
+ await dispatchEntry(editor, entry);
1405
+ if (queue.length > 0) throw new Error("unconsumed ids");
1406
+ }
1407
+ }
1408
+ /** Dispatch one journal entry to the matching editor method; payload is passed through. */
1409
+ async function dispatchEntry(editor, entry) {
1410
+ const payload = entry.payload;
1411
+ const options = entry.intent != null ? { intent: entry.intent } : void 0;
1412
+ switch (entry.kind) {
1413
+ case "MoveVideoClips":
1414
+ await editor.moveVideoClips(payload, options);
1415
+ return;
1416
+ case "MoveVideoClipsByAnchor":
1417
+ await editor.moveVideoClipsByAnchor(payload, options);
1418
+ return;
1419
+ case "DeleteVideoClips":
1420
+ await editor.deleteVideoClips(payload, options);
1421
+ return;
1422
+ case "AddVideoClips":
1423
+ await editor.addVideoClips(payload, options);
1424
+ return;
1425
+ case "AdjustVideoClipVolume":
1426
+ await editor.adjustVideoClipVolume(payload, options);
1427
+ return;
1428
+ case "SetVideoClipSpeedShift":
1429
+ await editor.setVideoClipSpeedShift(payload, options);
1430
+ return;
1431
+ case "ReplaceVideoClipContent":
1432
+ await editor.replaceVideoClipContent(payload, options);
1433
+ return;
1434
+ case "ReplaceVideoClipSequence":
1435
+ await editor.replaceVideoClipSequence(payload, options);
1436
+ return;
1437
+ case "AdjustVideoClipDuration":
1438
+ await editor.adjustVideoClipDuration(payload, options);
1439
+ return;
1440
+ case "AddSpeeches":
1441
+ await editor.addSpeeches(payload, options);
1442
+ return;
1443
+ case "DeleteSpeeches":
1444
+ await editor.deleteSpeeches(payload, options);
1445
+ return;
1446
+ case "MoveSpeeches":
1447
+ await editor.moveSpeeches(payload, options);
1448
+ return;
1449
+ case "MoveBgm":
1450
+ await editor.moveBgm(payload, options);
1451
+ return;
1452
+ case "ChangeSpeechScript":
1453
+ await editor.changeSpeechScript(payload, options);
1454
+ return;
1455
+ case "ChangeSpeechVoice":
1456
+ await editor.changeSpeechVoice(payload, options);
1457
+ return;
1458
+ case "AdjustSpeechVolume":
1459
+ await editor.adjustSpeechVolume(payload, options);
1460
+ return;
1461
+ case "SetCaptionVisibility":
1462
+ await editor.setCaptionVisibility(payload, options);
1463
+ return;
1464
+ case "SetCaptionStyle":
1465
+ await editor.setCaptionStyle(payload, options);
1466
+ return;
1467
+ case "SetBgm":
1468
+ await editor.setBgm(payload, options);
1469
+ return;
1470
+ case "DeleteBgm":
1471
+ await editor.deleteBgm(payload, options);
1472
+ return;
1473
+ case "AdjustBgmVolume":
1474
+ await editor.adjustBgmVolume(payload, options);
1475
+ return;
1476
+ default: {
1477
+ const _exhaustive = entry.kind;
1478
+ throw new Error(`replayJournal: unsupported kind ${String(_exhaustive)}`);
1479
+ }
1480
+ }
1481
+ }
1482
+ //#endregion
1911
1483
  //#region src/editor/types.ts
1912
1484
  /** Runtime list of the frozen, implemented kinds (for guards / introspection). */
1913
1485
  const IMPLEMENTED_SEMANTIC_OP_KINDS = [
@@ -1923,10 +1495,10 @@ const IMPLEMENTED_SEMANTIC_OP_KINDS = [
1923
1495
  "AddSpeeches",
1924
1496
  "DeleteSpeeches",
1925
1497
  "MoveSpeeches",
1498
+ "MoveBgm",
1926
1499
  "ChangeSpeechScript",
1927
1500
  "ChangeSpeechVoice",
1928
1501
  "AdjustSpeechVolume",
1929
- "ResizeCaption",
1930
1502
  "SetCaptionVisibility",
1931
1503
  "SetCaptionStyle",
1932
1504
  "SetBgm",
@@ -2103,6 +1675,18 @@ var ManualSyncDoc = class ManualSyncDoc {
2103
1675
  return fromVideoDocument(this.adapter.snapshot());
2104
1676
  }
2105
1677
  /**
1678
+ * Replay a deterministic sandbox journal into this document.
1679
+ *
1680
+ * The adapter stays private so callers cannot bypass the manual-sync
1681
+ * lifecycle; journal replay is the one host-side operation that needs the
1682
+ * adapter's transaction boundary and recorded id queue. Durability is still
1683
+ * explicit: call {@link push} after replay and report success only from its
1684
+ * verdict.
1685
+ */
1686
+ async replayJournal(journal) {
1687
+ await replayJournal(this.adapter, journal);
1688
+ }
1689
+ /**
2106
1690
  * Mark the document state the caller has just observed, for a later
2107
1691
  * {@link hasChangedSince}.
2108
1692
  *
@@ -2702,6 +2286,24 @@ var MengineDocSession = class {
2702
2286
  if (this.editorValue == null) throw new Error("mengine doc session is not started");
2703
2287
  return this.editorValue;
2704
2288
  }
2289
+ /**
2290
+ * Opaque version token of the local oplog (base64 `VersionVector.encode`).
2291
+ * Equality-comparable only: equal means no observed change (local or
2292
+ * remote-arrived) since the token was taken. Throws when not started.
2293
+ */
2294
+ version() {
2295
+ if (this.adapterValue == null) throw new Error("mengine doc session is not started");
2296
+ return bytesToBase64(this.adapterValue.doc.oplogVersion().encode());
2297
+ }
2298
+ /**
2299
+ * The live document adapter, exposed for journal replay (commit channel).
2300
+ * Replaying through it still goes SemanticEditor → Loro → mengine-server —
2301
+ * no write bypass. Typed by the narrow interface on purpose.
2302
+ */
2303
+ get documentAdapter() {
2304
+ if (this.adapterValue == null) throw new Error("mengine doc session is not started");
2305
+ return this.adapterValue;
2306
+ }
2705
2307
  /** Current document snapshot (read model). */
2706
2308
  snapshot() {
2707
2309
  if (this.adapterValue == null) throw new Error("mengine doc session is not started");
@@ -2812,4 +2414,4 @@ function serverCovers(serverVV, target) {
2812
2414
  return covers(server, target);
2813
2415
  }
2814
2416
  //#endregion
2815
- export { DEFAULT_UNIT_TIME_MS, IMPLEMENTED_SEMANTIC_OP_KINDS, LANE_KINDS_IN_STACK_ORDER, ManualSyncDoc, MedeoHttpDocStorage, MemoryDocStorage, MengineAckFailedError, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MenginePushRejectedError, MirrorVideoDocumentAdapter, SchemaValidator, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, VIDEO_DOCUMENT_SCHEMA_VERSION, ValidationError, VideoDocumentValidationError, arrangeMainTrackSeamlessly, assertValidVideoDocument, base64ToBytes, buildInitialVideoDocument, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, decodeDocVersionMark, derivePositionFromAbs, encodeDocVersionMark, ensureLaneTrack, fillMainTrackTimeGaps, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, laneTrackId, mainTrackRanges, partDurationMs, partUnionSchema, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, relativePositionForAbs, resolveAllSpeechOverlaps, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, snapshotToPlain, solveVideoDocument, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema };
2417
+ export { DEFAULT_UNIT_TIME_MS, IMPLEMENTED_SEMANTIC_OP_KINDS, LANE_KINDS_IN_STACK_ORDER, ManualSyncDoc, MedeoHttpDocStorage, MemoryDocStorage, MengineAckFailedError, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MenginePushRejectedError, MirrorVideoDocumentAdapter, PlainMemoryAdapter, SchemaValidator, SemanticEditor, TIMELINE_SKELETON_DURATION_MS, VIDEO_DOCUMENT_SCHEMA_VERSION, ValidationError, VideoDocumentValidationError, arrangeMainTrackSeamlessly, assertValidVideoDocument, base64ToBytes, buildInitialVideoDocument, buildSpeechHostMap, bytesToBase64, cascadeAfterVideoClipChanges, createEditSandbox, createMirrorVideoDocument, createMirrorVideoDocumentAdapter, createPlainMemoryAdapter, decodeDocVersionMark, derivePositionFromAbs, effectiveVideoClipDurationMs, encodeDocVersionMark, ensureLaneTrack, fillMainTrackTimeGaps, findLaneTrack, fromVideoDocument, generatePartId, getAt, hostForAbsMs, isEmptyVideoClip, isImplementedSemanticOpKind, isMap, laneTrackId, mainTrackRanges, partDurationMs, partUnionSchema, readMainTrackItems, readMengineEventStream, readPart, readPartDurationMs, readVideoDocumentFromDraft, reassignSpeechesToVideoClipsByTime, recalculateTimelineDuration, recoverOrphanedSpeechAnchorsByTime, relativePositionForAbs, replayJournal, resolveAllSpeechOverlaps, resolveAnchoredClipsTimePosition, resolveSpeechOverlapByShiftingVideos, safeDurationMs, schemas_exports as schemas, snapshotToPlain, solveVideoDocument, speedOf, syncAggregatedClipsTimePosition, toVideoDocument, validateVideoDocument, videoDocumentMirrorSchema, videoDocumentSchema, writeVideoDocumentToDraft };