@mengine/medeo-client 2.0.1 → 2.1.1-dsl.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,2538 +1,63 @@
1
- import { t as __exportAll } from "./chunk-D7D4PA-g.js";
2
- import { A as isEmptyVideoClip, C as fillMainTrackTimeGaps, D as resolveSpeechOverlapByShiftingVideos, E as resolveAllSpeechOverlaps, F as partUnionToDraft, I as recordEntries, L as videoDocumentMirrorSchema, M as safeDurationMs, N as DEFAULT_UNIT_TIME_MS, O as syncAggregatedClipsTimePosition, P as effectiveVideoClipDurationMs, R as base64ToBytes, S as arrangeMainTrackSeamlessly, T as recalculateTimelineDuration, _ as ensureLaneTrack, a as DocumentMutationGuard, b as solveVideoDocument, c as derivePositionFromAbs, d as VideoDocumentValidationError, f as assertValidVideoDocument, g as LANE_KINDS_IN_STACK_ORDER, h as videoDocumentSchema, i as readVideoDocumentFromDraft, j as partDurationMs, k as TIMELINE_SKELETON_DURATION_MS, l as fromVideoDocument, m as partUnionSchema, n as createMirrorVideoDocument, o as buildInitialVideoDocument, p as validateVideoDocument, r as createMirrorVideoDocumentAdapter, s as buildSpeechHostMap, t as MirrorVideoDocumentAdapter, u as toVideoDocument, v as findLaneTrack, w as reassignSpeechesToVideoClipsByTime, x as cascadeAfterVideoClipChanges, y as laneTrackId, z as bytesToBase64 } from "./document-B_JQwrC5.js";
3
- import { z } from "zod";
4
- import { LoroDoc, UndoManager, VersionVector } from "loro-crdt";
1
+ import { n as bytesToBase64, t as base64ToBytes } from "./base64-D-jd-dr5.js";
2
+ import { a as covers, c as readMengineEventStream, d as MenginePushRejectedError, i as openManualDocument, l as MengineHttpClient, n as MedeoHttpDocStorage, o as decodeDocVersionMark, r as ManualSyncTransport, s as encodeDocVersionMark, t as MemoryDocStorage, u as MengineHttpRequestError } from "./storage-BW3tI_ER.js";
3
+ import { C as resourceId, S as relationId, a as documentFormat, c as isInvalidAtomic, i as assertDslDocument, l as timeAnchorCodec, o as medeoDslLoroShape, r as UnsupportedDocumentFormatError, s as captionContentCodec, t as DEFAULT_UNIT_TIME_MS, w as tupleHash, x as defaultIdFactory } from "./video-draft-types-DTXxvPt-.js";
4
+ import { a as MedeoDsl, i as DslEditor, n as toVideoDraft, o as resolveDslLayout, r as createInitialMedeoDsl, s as DslProjectionError, t as fromVideoDraft } from "./dsl-Dkecq_2c.js";
5
+ import { UndoManager, VersionVector } from "loro-crdt";
5
6
  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/http-client.ts
9
- /**
10
- * Public route prefix of the mengine doc-collaboration API. Must stay in lockstep
11
- * with `apps/mengine-server`'s `API_PREFIX` — the two form the private wire
12
- * contract between this client and the relay server. Versioned, mengine-owned
13
- * namespace (mirrors the cluster's `/oapi/v1` precedent for an independent
14
- * subsystem) rather than folding into the shared `/api/v2/*` space.
15
- */
16
- const MENGINE_API_PREFIX = "/api/mengine/v1";
17
- var MengineHttpRequestError = class extends Error {
18
- status;
19
- payload;
20
- constructor(status, payload) {
21
- super(`mengine request failed: ${status}`);
22
- this.status = status;
23
- this.payload = payload;
24
- this.name = "MengineHttpRequestError";
25
- }
26
- };
27
- /**
28
- * A push the server refused on its merits (`kind: 'rejected'`), as opposed to a
29
- * transport failure. Carries the server's machine `code` so callers can branch on
30
- * *why* rather than on an HTTP status:
31
- *
32
- * - `missing_dependency` (server answers 409) — retryable: the update depends on
33
- * ops the server log lacks, so catching up and re-exporting resolves it.
34
- * - `corrupt_update` (server answers 422) — never valid, retrying cannot help.
35
- *
36
- * Distinct from {@link MengineHttpRequestError} (transport/status-level failure)
37
- * and from a raw `fetch` rejection (network down): the three are separate classes
38
- * so a caller can tell "the server said no" from "the server never answered".
39
- */
40
- var MenginePushRejectedError = class extends Error {
41
- code;
42
- serverMessage;
43
- serverVersion;
44
- status;
45
- constructor(code, serverMessage, serverVersion, status) {
46
- super(`mengine rejected update: ${code}: ${serverMessage}`);
47
- this.code = code;
48
- this.serverMessage = serverMessage;
49
- this.serverVersion = serverVersion;
50
- this.status = status;
51
- this.name = "MenginePushRejectedError";
52
- }
53
- };
54
- var MengineHttpClient = class {
55
- options;
56
- fetchImpl;
57
- constructor(options) {
58
- this.options = options;
59
- this.fetchImpl = options.fetchImpl ?? globalThis.fetch.bind(globalThis);
60
- }
61
- async fetchSnapshot() {
62
- return await this.requestJson("snapshot");
63
- }
64
- /**
65
- * Loro VV-diff pull: send the caller's oplog `VersionVector.encode` as `from`
66
- * (omit for a full pull) and receive exactly the updates it is missing plus
67
- * the server's current VV. Replaces the old integer `after_update_id` cursor.
68
- */
69
- async sync(fromVV) {
70
- const query = fromVV != null ? `?from=${encodeURIComponent(bytesToBase64(fromVV))}` : "";
71
- return await this.requestJson(`sync${query}`);
72
- }
73
- /** Audit trail: extracted metadata per accepted update, in log order. */
74
- async audit() {
75
- return await this.requestJson("audit");
76
- }
77
- /**
78
- * Append one Loro update to the server log.
79
- *
80
- * Resolves for both accepted outcomes and hands the verdict back verbatim —
81
- * `ack` (appended, `update_seq` allocated) and `duplicate` (already known, no
82
- * row appended). A `duplicate` is NOT an error, but it is also not an ack: it
83
- * means the bytes contributed nothing, so a caller waiting for its own write to
84
- * land must be able to tell them apart. Hence the verdict is returned rather
85
- * than collapsed into `void`.
86
- *
87
- * Rejections raise {@link MenginePushRejectedError} carrying the server's
88
- * machine `code`. The server answers them with 409/422, so the failure arrives
89
- * as a non-ok response; this method re-reads the parsed body to recover `code` /
90
- * `server_version` instead of leaving the caller a bare status. Transport-level
91
- * failures stay {@link MengineHttpRequestError}, and a dead network keeps
92
- * surfacing as the underlying `fetch` rejection.
93
- */
94
- async pushUpdate(update) {
95
- let response;
96
- try {
97
- response = await this.requestJson("updates", {
98
- method: "POST",
99
- body: JSON.stringify({ update: bytesToBase64(update) })
100
- });
101
- } catch (error) {
102
- throw asPushRejection(error) ?? error;
103
- }
104
- if (response.kind === "rejected") throw new MenginePushRejectedError(response.code, response.message, response.server_version, void 0);
105
- return response;
106
- }
107
- eventsUrl() {
108
- return this.endpoint("events");
109
- }
110
- headers() {
111
- const headers = new Headers();
112
- headers.set("accept", "application/json");
113
- headers.set("content-type", "application/json");
114
- const authToken = typeof this.options.authToken === "function" ? this.options.authToken() : this.options.authToken;
115
- if (authToken != null && authToken !== "") headers.set("authorization", `Bearer ${authToken}`);
116
- const userId = typeof this.options.userId === "function" ? this.options.userId() : this.options.userId;
117
- if (userId != null && userId !== "") headers.set("medeo-user-id", userId);
118
- return headers;
119
- }
120
- async fetch(input, init) {
121
- return await this.fetchImpl(input, init);
122
- }
123
- async requestJson(path, init = {}) {
124
- const headers = this.headers();
125
- new Headers(init.headers).forEach((value, key) => {
126
- headers.set(key, value);
127
- });
128
- const response = await this.fetchImpl(this.endpoint(path), {
129
- ...init,
130
- headers
131
- });
132
- const payload = await safeReadJson(response);
133
- if (!response.ok) throw new MengineHttpRequestError(response.status, payload);
134
- return payload;
135
- }
136
- endpoint(path) {
137
- return `${this.options.httpOrigin.replace(/\/$/, "")}${MENGINE_API_PREFIX}/docs/${encodeURIComponent(this.options.docId)}/${path}`;
138
- }
139
- };
140
- /**
141
- * Recover a typed rejection from a failed request: the server sends `rejected`
142
- * bodies with a 409/422 status, so they surface as {@link MengineHttpRequestError}
143
- * whose payload still carries the machine `code`. Returns `undefined` for any
144
- * other failure so the caller rethrows the original.
145
- */
146
- function asPushRejection(error) {
147
- if (!(error instanceof MengineHttpRequestError)) return void 0;
148
- const payload = error.payload;
149
- if (payload == null || typeof payload !== "object") return void 0;
150
- const body = payload;
151
- if (body.kind !== "rejected" || typeof body.code !== "string") return void 0;
152
- return new MenginePushRejectedError(body.code, body.message ?? "", body.server_version, error.status);
153
- }
154
- async function safeReadJson(response) {
155
- const text = await response.text();
156
- if (text.length === 0) return null;
157
- try {
158
- return JSON.parse(text);
159
- } catch {
160
- return text;
161
- }
162
- }
163
- //#endregion
164
- //#region src/client/sse.ts
165
- async function readMengineEventStream(options) {
166
- const response = await options.client.fetch(options.client.eventsUrl(), {
167
- headers: options.client.headers(),
168
- signal: options.signal
169
- });
170
- if (!response.ok) throw new Error(`mengine event stream failed: ${response.status}`);
171
- const reader = response.body?.getReader();
172
- if (reader == null) throw new Error("mengine event stream response has no readable body");
173
- options.onOpen?.();
174
- const decoder = new TextDecoder();
175
- let buffer = "";
176
- while (options.signal?.aborted !== true) {
177
- const { done, value } = await reader.read();
178
- if (done) return;
179
- buffer += decoder.decode(value, { stream: true });
180
- let splitAt = findSseFrameBoundary(buffer);
181
- while (splitAt >= 0) {
182
- const frame = buffer.slice(0, splitAt);
183
- buffer = buffer.slice(buffer[splitAt] === "\r" ? splitAt + 4 : splitAt + 2);
184
- handleSseFrame(frame, options);
185
- splitAt = findSseFrameBoundary(buffer);
186
- }
187
- }
188
- }
189
- function handleSseFrame(frame, options) {
190
- const event = parseSseFrame(frame);
191
- if (event.event === "resync") {
192
- options.onResync?.();
193
- return;
194
- }
195
- if (event.event !== "update" || event.data.length === 0) return;
196
- options.onUpdate(JSON.parse(event.data.join("\n")));
197
- }
198
- function parseSseFrame(frame) {
199
- let event = null;
200
- const data = [];
201
- for (const line of frame.split(/\r?\n/)) if (line.startsWith("event:")) event = line.slice(6).trim();
202
- else if (line.startsWith("data:")) data.push(line.slice(5).trimStart());
203
- return {
204
- event,
205
- data
206
- };
207
- }
208
- function findSseFrameBoundary(buffer) {
209
- const lf = buffer.indexOf("\n\n");
210
- const crlf = buffer.indexOf("\r\n\r\n");
211
- if (lf < 0) return crlf;
212
- if (crlf < 0) return lf;
213
- return Math.min(lf, crlf);
214
- }
215
- //#endregion
216
- //#region src/editor/id-gen.ts
217
- /**
218
- * Part-id generation, aligned with the online ecosystem.
219
- *
220
- * The authoritative online producers — agent-harness (`@harness/shared`
221
- * `genObjId`) and director.v2 (`common/obj_id.py` `gen_obj_id`) — both mint part
222
- * ids as `` `${prefix}_${ulid()}` ``, and real captured drafts use exactly that
223
- * shape (`clip_…` / `spe_…` / `cap_…` / `bgm_…`, each a 26-char ULID). The engine
224
- * previously emitted `vc_<base36 timestamp><6 random>`, a different prefix AND a
225
- * different encoding — the sole cross-repo id divergence. This module removes it
226
- * by emitting the same `<prefix>_<ULID>` bytes.
227
- *
228
- * The ULID is generated inline (Crockford Base32, 48-bit time + 80-bit random)
229
- * rather than pulling the `ulid` npm package: the randomness class matches the
230
- * old generator (both `Math.random`-based) and it keeps `@mengine/medeo-client`
231
- * dependency-free for a purely mechanical id string. Part ids only need to be
232
- * unique and lexicographically time-sortable, which this satisfies.
233
- */
234
- /** Crockford Base32 alphabet (no I, L, O, U), per the ULID spec. */
235
- const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
236
- const TIME_LEN = 10;
237
- const RANDOM_LEN = 16;
238
- function encodeTime(now) {
239
- let out = "";
240
- let ms = now;
241
- for (let i = TIME_LEN - 1; i >= 0; i--) {
242
- const mod = ms % 32;
243
- out = CROCKFORD[mod] + out;
244
- ms = (ms - mod) / 32;
245
- }
246
- return out;
247
- }
248
- function encodeRandom() {
249
- let out = "";
250
- for (let i = 0; i < RANDOM_LEN; i++) out += CROCKFORD[Math.floor(Math.random() * 32)];
251
- return out;
252
- }
253
- /** A 26-char Crockford Base32 ULID (10-char time + 16-char random). */
254
- function ulid() {
255
- return encodeTime(Date.now()) + encodeRandom();
256
- }
257
- function generatePartId(prefix) {
258
- return `${prefix}_${ulid()}`;
259
- }
260
- //#endregion
261
- //#region src/editor/schemas/shared.ts
262
- const clipIdSchema = z.string().min(1).describe("The clip part ID on the timeline");
263
- 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)");
264
- const mediaIdSchema = z.string().min(1).describe("The media asset ID");
265
- const speechIdSchema = z.string().min(1).describe("The speech part ID on the timeline");
266
- const timelineMsSchema = z.number().int().min(0).describe("Time position in milliseconds on the timeline (>= 0)");
267
- const positiveMsSchema = z.number().int().positive().describe("Duration in milliseconds (> 0)");
268
- const volumeSchema = z.number().min(-60).max(20).describe("Volume in decibels (-60.0 to 20.0; 0.0 = original, -60 = mute, +20 = max)");
269
- 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)");
270
- const tangentHandleSchema = z.object({
271
- x: z.number().finite(),
272
- y: z.number().finite()
273
- }).describe("Bezier tangent handle (x, y)");
274
- const speedKeyframeSchema = z.object({
275
- position: z.number().min(0).max(1),
276
- rate: z.number().min(0),
277
- in_tangent: tangentHandleSchema.optional(),
278
- out_tangent: tangentHandleSchema.optional()
279
- }).describe("A speed keyframe: normalized position (0..1), rate, optional tangents");
280
- /**
281
- * A clip's playback-speed fact, the only thing `SetVideoClipSpeedShift` writes.
282
- * Mirrors the IDL `SpeedShift`: `category` is `linear` | `curve`, and `config`
283
- * is a discriminated union — `{ linear: { speed } }` for a constant multiplier
284
- * (the multiplier projection reads at `config.linear.speed`) or `{ curve: {
285
- * keyframes } }` for a Bezier-controlled variable speed (RFC 02 / `reference/16`
286
- * §0). Exactly one of `linear` / `curve` is present.
287
- */
288
- const speedShiftSchema = z.object({
289
- category: z.enum(["linear", "curve"]),
290
- mode: z.string(),
291
- config: z.union([z.object({ linear: z.object({ speed: z.number().finite().positive() }) }), z.object({ curve: z.object({ keyframes: z.array(speedKeyframeSchema).min(2) }) })])
292
- }).describe("Speed shift: linear multiplier or Bezier curve, mirroring the IDL shape");
293
- const voiceSchema = z.object({
294
- id: z.string().min(1),
295
- name: z.string()
296
- }).describe("TTS voice summary attached to a speech");
297
- //#endregion
298
- //#region src/editor/schemas/speech-assets.ts
299
- /**
300
- * The materialized TTS result shared by `AddSpeeches` / `ChangeSpeechScript` /
301
- * `ChangeSpeechVoice` (see `results/phase-4-side-effect-payload-contract.md`
302
- * §1/§2). The side effect (TTS/ASR + billing) runs upstream; the op receives the
303
- * stable speech + caption parts and writes them as authoritative facts. No
304
- * cascade runs on write — the projection derives absolute positions on read.
305
- *
306
- * Each speech carries the anchoring fact directly (RFC 02 §4): the host video
307
- * clip `anchor_part_id` and the `offset_ms` within it. The upstream caller
308
- * already knows which clip a speech attaches to, so the op writes
309
- * `{ mode:'anchored', anchorPartId, offsetMs }` verbatim — no write-time
310
- * host-picking. Captions anchor to their speech via the caption part's
311
- * `start_ms` (offset within the speech).
312
- */
313
- const speechAssetSchema = z.object({
314
- speech_id: speechIdSchema.describe("The speech part ID (= side-effect speech_parts[].id)"),
315
- anchor_part_id: clipIdSchema.describe("Host video clip part ID the speech anchors to (RFC 02 §4)"),
316
- offset_ms: timelineMsSchema.describe("Offset within the host clip (speech.abs = host.abs + offset_ms)"),
317
- audio_storage_key: z.string().min(1),
318
- duration_ms: positiveMsSchema,
319
- audio_script: z.string(),
320
- volume: volumeSchema,
321
- voice: voiceSchema,
322
- origin_speech_id: z.string().min(1),
323
- caption_ids: z.array(z.string().min(1)).describe("Caption part IDs owned by this speech")
324
- });
325
- const captionAssetSchema = z.object({
326
- caption_id: z.string().min(1).describe("The caption part ID (= side-effect created_caption_parts[].id)"),
327
- speech_part_id: speechIdSchema.describe("The owning speech part ID"),
328
- text: z.string(),
329
- start_ms: timelineMsSchema.describe("Offset within the host speech (caption.abs = speech.abs + start_ms)"),
330
- duration_ms: positiveMsSchema
331
- });
332
- /** A materialized speech-subtree write (speeches + their captions). */
333
- const speechAssetsSchema = z.object({
334
- speeches: z.array(speechAssetSchema).min(1).describe("Materialized speech parts to write"),
335
- captions: z.array(captionAssetSchema).describe("Materialized caption parts owned by the speeches")
336
- });
337
- //#endregion
338
- //#region src/editor/schemas/add-speeches.ts
339
- /**
340
- * Add speeches (and their captions). TTS runs upstream; the stable speech /
341
- * caption parts arrive materialized (see `speech-assets.ts`). The op writes the
342
- * parts and each speech's `{ mode:'anchored', anchorPartId, offsetMs }` fact
343
- * verbatim — no write-time host-picking, no cascade (RFC 02 §4). The projection
344
- * derives absolute positions on read.
345
- */
346
- const addSpeechesInputSchema = speechAssetsSchema.describe("Materialized speeches + captions to add");
347
- //#endregion
348
- //#region src/editor/schemas/add-video-clips.ts
349
- /**
350
- * Add video clips to a track. Each clip's duration facts are separated so a
351
- * single number is never overloaded (RFC 02 / `reference/16` §0b):
352
- *
353
- * - `media_duration_ms` is the source media's intrinsic full length (a resource
354
- * fact, written to the part);
355
- * - `play_in` / `play_out` are the optional trim window into that media; when
356
- * omitted the whole media is used (`play_in=0`, `play_out=media_duration_ms`).
357
- *
358
- * The clip's effective timeline duration is derived by the projection from the
359
- * trim window and `speed_shift` — it is never an input here.
360
- */
361
- const addVideoClipsInputSchema = z.object({
362
- clips: z.array(z.object({
363
- media_id: mediaIdSchema.describe("The media asset ID for the video clip"),
364
- start_ms: timelineMsSchema.optional().describe("Absolute start time in milliseconds on the timeline"),
365
- media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
366
- play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
367
- play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)"),
368
- track_id: z.string().min(1).optional().describe("Target track ID (optional, defaults to main track)")
369
- })).min(1).describe("List of video clips to create"),
370
- before_clip_id: z.string().min(1).optional().describe("Insert new clips before this clip ID"),
371
- after_clip_id: z.string().min(1).optional().describe("Insert new clips after this clip ID")
372
- }).superRefine((data, ctx) => {
373
- if (data.before_clip_id != null && data.after_clip_id != null) {
374
- ctx.addIssue({
375
- code: z.ZodIssueCode.custom,
376
- message: "Cannot provide both before_clip_id and after_clip_id"
377
- });
378
- return;
379
- }
380
- const hasRelative = data.before_clip_id != null || data.after_clip_id != null;
381
- for (let i = 0; i < data.clips.length; i++) {
382
- const clip = data.clips[i];
383
- if (hasRelative && clip.start_ms != null) ctx.addIssue({
384
- code: z.ZodIssueCode.custom,
385
- message: `clips[${i}].start_ms must not be provided when using before_clip_id or after_clip_id`,
386
- path: [
387
- "clips",
388
- i,
389
- "start_ms"
390
- ]
391
- });
392
- if (!hasRelative && clip.start_ms == null) ctx.addIssue({
393
- code: z.ZodIssueCode.custom,
394
- message: `clips[${i}].start_ms is required when not using relative positioning`,
395
- path: [
396
- "clips",
397
- i,
398
- "start_ms"
399
- ]
400
- });
401
- const playIn = clip.play_in ?? 0;
402
- const playOut = clip.play_out ?? clip.media_duration_ms;
403
- if (playOut > clip.media_duration_ms) ctx.addIssue({
404
- code: z.ZodIssueCode.custom,
405
- message: `clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
406
- path: [
407
- "clips",
408
- i,
409
- "play_out"
410
- ]
411
- });
412
- if (playIn >= playOut) ctx.addIssue({
413
- code: z.ZodIssueCode.custom,
414
- message: `clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
415
- path: [
416
- "clips",
417
- i,
418
- "play_in"
419
- ]
420
- });
421
- }
422
- });
423
- //#endregion
424
- //#region src/editor/schemas/adjust-bgm-volume.ts
425
- const adjustBgmVolumeInputSchema = z.object({ bgm: z.array(z.object({
426
- bgm_id: clipIdSchema.describe("The bgm part ID to adjust volume for"),
427
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
428
- })).min(1).describe("List of bgm parts with their new volume settings") });
429
- //#endregion
430
- //#region src/editor/schemas/adjust-speech-volume.ts
431
- const adjustSpeechVolumeInputSchema = z.object({ speeches: z.array(z.object({
432
- speech_id: speechIdSchema.describe("The speech part ID to adjust volume for"),
433
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
434
- })).min(1).describe("List of speeches with their new volume settings") });
435
- //#endregion
436
- //#region src/editor/schemas/adjust-video-clip-duration.ts
437
- /**
438
- * Re-trim existing video clips (the user-facing "adjust duration" gesture is a
439
- * trim of the source window). The new `play_in` / `play_out` are the facts; the
440
- * effective timeline duration is derived from them and the clip's `speed_shift`,
441
- * and the change reflows downstream clips, speeches, and the timeline inside the
442
- * op's transaction (no caller-materialized cascade).
443
- */
444
- const adjustVideoClipDurationInputSchema = z.object({ clips: z.array(z.object({
445
- clip_id: clipIdSchema.describe("The video clip part ID to re-trim"),
446
- play_in: timelineMsSchema.describe("New trim window start in the source media"),
447
- play_out: positiveMsSchema.describe("New trim window end in the source media")
448
- })).min(1).describe("Video clips with their new trim windows") });
449
- //#endregion
450
- //#region src/editor/schemas/adjust-video-clip-volume.ts
451
- const adjustVideoClipVolumeInputSchema = z.object({ clips: z.array(z.object({
452
- clip_id: clipIdSchema.describe("The video clip part ID to adjust volume for"),
453
- volume: volumeSchema.describe("Volume in decibels (-60.0 to 20.0; 0.0 = original)")
454
- })).min(1).describe("List of video clips with their new volume settings") });
455
- //#endregion
456
- //#region src/editor/schemas/change-speech.ts
457
- /**
458
- * Change a speech's script or voice. Both re-run TTS upstream and return the
459
- * regenerated speech / caption parts in the same materialized shape as
460
- * `AddSpeeches` (`speech-assets.ts`); the op upserts them by id (the speech part
461
- * id is preserved across a re-TTS), re-seats at `start_ms`, and reflows. Old
462
- * caption parts no longer owned by the speech are removed via `caption_ids`.
463
- */
464
- const changeSpeechScriptInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new script)");
465
- const changeSpeechVoiceInputSchema = speechAssetsSchema.describe("Regenerated speeches + captions (new voice)");
466
- //#endregion
467
- //#region src/editor/schemas/delete-bgm.ts
468
- /**
469
- * Remove the document BGM. Pure document edit: clears the bgm lane and removes
470
- * the bgm part. Takes no input (a document holds at most one bgm); an empty
471
- * object keeps the op signature uniform with the rest.
472
- */
473
- const deleteBgmInputSchema = z.object({}).describe("Remove the document BGM (no parameters)");
474
- //#endregion
475
- //#region src/editor/schemas/delete-speeches.ts
476
- /**
477
- * Delete speeches with their captions. Pure document edit (no side effect): the
478
- * op removes each speech part, cascade-deletes the captions it owns (via
479
- * `caption_ids` / `speech_part_id`), drops their track items, and reflows.
480
- */
481
- const deleteSpeechesInputSchema = z.object({ speech_ids: speechIdsSchema.describe("Speech part IDs to delete (their captions cascade-delete)") });
482
- //#endregion
483
- //#region src/editor/schemas/delete-video-clips.ts
484
- /**
485
- * How a delete handles the anchored subtree (speeches anchored to a deleted clip,
486
- * and their captions) — a delete-op policy, not a data-model field (reference/17
487
- * §6). `cascade` (default) removes the subtree; `detach` keeps the direct
488
- * anchored children, re-pinning them to `absolute` so they stay on the timeline.
489
- */
490
- const anchoredDeletePolicySchema = z.enum(["cascade", "detach"]);
491
- const deleteVideoClipsInputSchema = z.object({
492
- clip_ids: clipIdsSchema.describe("List of video clip part IDs to delete from the main track"),
493
- on_anchored: anchoredDeletePolicySchema.optional().describe("How to treat anchored children (default cascade)")
494
- });
495
- //#endregion
496
- //#region src/editor/schemas/move-speeches.ts
497
- /**
498
- * Move speeches in time. Pure document edit: the op re-seats each speech at its
499
- * new absolute `start_ms`; the cascade reassigns it to the host video clip,
500
- * resolves overlaps, and reflows. Captions follow their speech.
501
- */
502
- const moveSpeechesInputSchema = z.object({ speeches: z.array(z.object({
503
- speech_id: speechIdSchema.describe("The speech part ID to move"),
504
- new_start_ms: timelineMsSchema.describe("New absolute start time on the timeline")
505
- })).min(1).describe("Speeches to move to new positions") });
506
- //#endregion
507
- //#region src/editor/schemas/move-video-clips-by-anchor.ts
508
- /**
509
- * Where the moved block lands on the main track.
510
- *
511
- * A discriminated union rather than two optional `before_clip_id` /
512
- * `after_clip_id` fields (the shape `addVideoClips` had to use, because there the
513
- * two modes share a whole clip description): here the alternatives carry nothing
514
- * in common, so making them mutually exclusive *by type* removes three runtime
515
- * `superRefine` checks that would otherwise have to be written and tested.
516
- *
517
- * `track_start` is an explicit member, not the absence of an anchor. The
518
- * agent-harness mutation this maps from treats "neither anchor given" as
519
- * "move to the front" (`applyBatchMoveVideoClips` falls back to `insertIndex = 0`),
520
- * which is a default buried in a tool description. Requiring the caller to name
521
- * that intent keeps a forgotten field from silently reordering the timeline.
522
- */
523
- const moveAnchorSchema = z.discriminatedUnion("position", [
524
- z.object({
525
- position: z.literal("before"),
526
- clip_id: clipIdSchema.describe("The moved block lands immediately before this clip")
527
- }),
528
- z.object({
529
- position: z.literal("after"),
530
- clip_id: clipIdSchema.describe("The moved block lands immediately after this clip")
531
- }),
532
- z.object({ position: z.literal("track_start") })
533
- ]).describe("Where the moved block lands: before/after a reference clip, or at the head of the track");
534
- /**
535
- * What happens to the speeches anchored to the clips being moved.
536
- *
537
- * - `follow` keeps each speech anchored where it is, so it travels with its clip
538
- * to the new position. In the anchored model this is the *no-op* branch: a
539
- * speech's authoritative fact is `{ anchorPartId, offsetMs }` and its absolute
540
- * time is derived on read from the host's position, so moving the host moves
541
- * the speech with no write to the speech at all.
542
- * - `keep_absolute` preserves each speech's current absolute landing instead, then
543
- * re-anchors it to whichever clip now covers that time (RFC 02 §9.1/§11.1). This
544
- * is the branch that costs an extra pass, and the one the FE timeline uses.
545
- *
546
- * **Required, with no default**, matching `deleteVideoClips` and
547
- * `replaceVideoClipSequence`. The two branches decide which picture the user's
548
- * narration ends up over, which is too consequential to infer from a missing
549
- * field — and neither branch is "safe enough" to be the implicit one.
550
- */
551
- const movedClipAnchoredPolicySchema = z.enum(["follow", "keep_absolute"]);
552
- /**
553
- * Reorder a set of main-track clips relative to a reference clip.
554
- *
555
- * Distinct from `moveVideoClips`, which positions clips by absolute time
556
- * (`new_start_ms`) and is what the FE timeline dispatches after a drag. Main-track
557
- * clips are `sequential`-positioned, so absolute time is not authoritative state
558
- * there (RFC 02 §4/§7): `moveVideoClips` has to *guess* an index back out of the
559
- * time it was handed, whereas an anchor already is the ordinal fact being changed.
560
- * Keeping them separate also isolates blast radius — this method can diverge on
561
- * speech policy without touching the FE path.
562
- *
563
- * `clip_ids` need not be contiguous. They move as one block, keeping their
564
- * relative order, which is the agent-harness `batch_move_video_clips` contract.
565
- */
566
- const moveVideoClipsByAnchorInputSchema = z.object({
567
- clip_ids: clipIdsSchema.describe("Clips to move as one block, keeping their relative order. Need not be contiguous on the track."),
568
- anchor: moveAnchorSchema,
569
- on_anchored: movedClipAnchoredPolicySchema.describe("What happens to speeches anchored to the moved clips (required — see the policy doc)")
570
- });
571
- //#endregion
572
- //#region src/editor/schemas/move-video-clips.ts
573
- const moveVideoClipsInputSchema = z.object({ clips: z.array(z.object({
574
- clip_id: clipIdSchema.describe("The video clip part ID to move"),
575
- new_start_ms: timelineMsSchema.describe("New absolute start time in milliseconds on the timeline"),
576
- new_track_id: z.string().min(1).optional().describe("Target track ID to move the clip to (optional)")
577
- })).min(1).describe("List of video clips to move to new positions") });
578
- //#endregion
579
- //#region src/editor/schemas/replace-video-clip-content.ts
580
- /**
581
- * Replace the media backing existing video clips. The media import runs upstream
582
- * (Director); its stable result — the new media id, intrinsic length, and the
583
- * reset trim window — arrives materialized (see
584
- * `results/phase-4-side-effect-payload-contract.md` §4). Director resets
585
- * `play_in=0` / `play_out=media_duration_ms` and clears `speed_shift` on
586
- * replacement. The clip `part_id`s (hence their track items) are unchanged; the
587
- * editor reflows the main track from the new effective durations.
588
- */
589
- const replaceVideoClipContentInputSchema = z.object({ clips: z.array(z.object({
590
- clip_id: clipIdSchema.describe("Existing video clip part ID to re-point"),
591
- origin_media_id: mediaIdSchema.describe("The new media asset ID"),
592
- media_duration_ms: positiveMsSchema.describe("The new media's intrinsic full length"),
593
- play_in: timelineMsSchema.describe("Trim window start in the new media (usually 0)"),
594
- play_out: positiveMsSchema.describe("Trim window end in the new media (usually = media_duration_ms)"),
595
- volume: volumeSchema
596
- })).min(1).describe("Video clips whose media is being replaced") });
597
- //#endregion
598
- //#region src/editor/schemas/replace-video-clip-sequence.ts
599
- /**
600
- * What happens to the speeches anchored to the clips being replaced.
601
- *
602
- * - `remap` re-anchors each surviving speech to the new clip in the SAME POSITION
603
- * of the sequence, keeping its offset — old[i]'s children become new[i]'s
604
- * children. An old clip with no counterpart (fewer new clips than old) has its
605
- * subtree deleted, because there is nothing left to anchor to.
606
- * - `cascade` deletes every anchored speech (and its captions) outright, like
607
- * `deleteVideoClips`.
608
- *
609
- * **Required, with no default.** The two branches differ in whether the user's
610
- * narration survives, and the agent tool that drives this op makes its
611
- * `preserve_speeches` flag required for that reason. A default here would let a
612
- * caller that forgot the field silently delete speech.
613
- */
614
- const anchoredReplacePolicySchema = z.enum(["remap", "cascade"]);
615
- /**
616
- * Replace a contiguous run of main-track clips with a new run.
617
- *
618
- * A composite of delete + insert that cannot be expressed as the two ops in
619
- * sequence, because the anchored speeches have to survive *across* the swap: with
620
- * `remap` they are re-anchored positionally, which needs both the old and the new
621
- * ids in the same transaction (ADR 0009 — the cascade stays in the editor, callers
622
- * never re-wire anchors themselves).
623
- *
624
- * Duration facts follow `addVideoClips`: `media_duration_ms` is the source's
625
- * intrinsic length and the trim window defaults to the whole media. The effective
626
- * timeline duration is derived by the projection, never an input.
627
- *
628
- * `media_id` is optional: omitting it creates a **deliberate empty placeholder
629
- * clip** (`origin_media_id: ''`) — structure with no picture. This is the only op
630
- * that can produce one, and it is authoritative state, unlike the gap fillers the
631
- * read-side solve mints (which never enter the document).
632
- */
633
- const replaceVideoClipSequenceInputSchema = z.object({
634
- old_clip_ids: clipIdsSchema.describe("The clips being replaced: a contiguous main-track run, listed in timeline order"),
635
- new_clips: z.array(z.object({
636
- media_id: mediaIdSchema.optional().describe("The replacement media asset ID. Omit to create an empty placeholder clip."),
637
- media_duration_ms: positiveMsSchema.describe("The source media's intrinsic full length in ms"),
638
- play_in: timelineMsSchema.optional().describe("Trim window start in the media (default 0)"),
639
- play_out: positiveMsSchema.optional().describe("Trim window end in the media (default media_duration_ms)")
640
- })).min(1).describe("The replacement clips, in the order they take on the track"),
641
- on_anchored: anchoredReplacePolicySchema.describe("What happens to speeches anchored to the replaced clips (required — see the policy doc)")
642
- }).superRefine((data, ctx) => {
643
- for (let i = 0; i < data.new_clips.length; i++) {
644
- const clip = data.new_clips[i];
645
- const playIn = clip.play_in ?? 0;
646
- const playOut = clip.play_out ?? clip.media_duration_ms;
647
- if (playOut > clip.media_duration_ms) ctx.addIssue({
648
- code: z.ZodIssueCode.custom,
649
- message: `new_clips[${i}].play_out ${playOut}ms exceeds media_duration_ms ${clip.media_duration_ms}ms`,
650
- path: [
651
- "new_clips",
652
- i,
653
- "play_out"
654
- ]
655
- });
656
- if (playIn >= playOut) ctx.addIssue({
657
- code: z.ZodIssueCode.custom,
658
- message: `new_clips[${i}].play_in ${playIn}ms must be less than play_out ${playOut}ms`,
659
- path: [
660
- "new_clips",
661
- i,
662
- "play_in"
663
- ]
664
- });
665
- }
666
- });
667
- //#endregion
668
- //#region src/editor/schemas/set-bgm.ts
669
- /**
670
- * Set the document BGM. The media's stable result (storage key) arrives
671
- * materialized from upstream (see
672
- * `results/phase-4-side-effect-payload-contract.md` §3). The op upserts the bgm
673
- * part and seats it on the bgm lane; its effective length is always the whole
674
- * timeline, derived by the projection on read — so there is no `duration_ms`
675
- * input or fact (RFC 02 / `reference/16` §0b). A `bgm_id` lets the op replace an
676
- * existing bgm part by id.
677
- */
678
- const setBgmInputSchema = z.object({
679
- bgm_id: z.string().min(1).describe("The bgm part ID to write"),
680
- audio_storage_key: z.string().min(1),
681
- origin_media_id: mediaIdSchema,
682
- volume: volumeSchema
683
- });
684
- //#endregion
685
- //#region src/editor/schemas/set-caption-style.ts
686
- /**
687
- * Set the caption visual style. GLOBAL by design: the style applies to every
688
- * caption part in the document — it carries NO `caption_id`. This mirrors the FE,
689
- * whose caption-style store (`caption-style.ts:persistCaptionStylePatch`) iterates
690
- * ALL captions and writes the same normalized style to each; the product has a
691
- * single document-wide caption style, not per-caption styling.
692
- *
693
- * Every field is optional and maps to a `CaptionStyle` attribute (snake_case
694
- * IDL). A field present in the input is written to every caption; a field ABSENT
695
- * from the input is left untouched on each caption (the editor merges the patch
696
- * onto each caption's existing style — this is a value edit, not a full-style
697
- * replace, so a partial patch such as "recolor only" does not wipe font size).
698
- *
699
- * Pure document edit, no cascade — captions keep their positions; only the style
700
- * sub-map of each caption part changes.
701
- */
702
- const setCaptionStyleInputSchema = z.object({
703
- font_id: z.string().min(1).optional().describe("Font ID referencing a font from the font library"),
704
- font_size: z.number().positive().optional().describe("Font size in points"),
705
- font_color: z.string().min(1).optional().describe("Font color as hex string, e.g. \"#FFFFFF\""),
706
- font_weight: z.number().int().optional().describe("Numeric font weight, e.g. 400 or 700"),
707
- entrance_animation: z.string().optional().describe("Entrance animation preset ID, e.g. \"fade\" or \"none\""),
708
- entrance_animation_duration_ms: z.number().min(0).optional().describe("Entrance animation duration in ms"),
709
- stroke_color: z.string().min(1).optional().describe("Outline/stroke color as hex string, e.g. \"#000000\""),
710
- stroke_width: z.number().min(0).optional().describe("Outline/stroke width in pixels"),
711
- position_x: z.number().optional().describe("Caption center X as a fraction (0.0 to 1.0)"),
712
- position_y: z.number().optional().describe("Caption center Y as a fraction (0.0 to 1.0)")
713
- }).describe("Document-wide caption style patch (no caption_id; applies to every caption)");
714
- //#endregion
715
- //#region src/editor/schemas/set-caption-visibility.ts
716
- /**
717
- * Toggle caption visibility (the caption track's `is_hidden` flag). Pure
718
- * document edit, no cascade — captions keep their positions; only the lane's
719
- * hidden flag changes.
720
- */
721
- const setCaptionVisibilityInputSchema = z.object({ is_hidden: z.boolean().describe("Whether the caption track is hidden") });
722
- //#endregion
723
- //#region src/editor/schemas/set-video-clip-speed-shift.ts
724
- /**
725
- * Set the playback speed of existing video clips. Per the speed-shift decision
726
- * (`reference/16` §0): the op writes only the `speed_shift` fact — it does NOT
727
- * store an effective `duration_ms` (projection derives it from the trim window /
728
- * speed) and does NOT scale anchored speeches' relative offsets (offsets stay
729
- * put; the cascade reflows absolute positions). A `null` speed_shift clears the
730
- * speed back to original (1×).
731
- */
732
- const setVideoClipSpeedShiftInputSchema = z.object({ clips: z.array(z.object({
733
- clip_id: clipIdSchema.describe("The video clip part ID to set speed for"),
734
- speed_shift: speedShiftSchema.nullable().describe("The new speed setting, or null to reset to 1×")
735
- })).min(1).describe("Video clips with their new speed settings") });
736
- //#endregion
737
- //#region src/editor/schemas/index.ts
738
- var schemas_exports = /* @__PURE__ */ __exportAll({
739
- addSpeechesInputSchema: () => addSpeechesInputSchema,
740
- addVideoClipsInputSchema: () => addVideoClipsInputSchema,
741
- adjustBgmVolumeInputSchema: () => adjustBgmVolumeInputSchema,
742
- adjustSpeechVolumeInputSchema: () => adjustSpeechVolumeInputSchema,
743
- adjustVideoClipDurationInputSchema: () => adjustVideoClipDurationInputSchema,
744
- adjustVideoClipVolumeInputSchema: () => adjustVideoClipVolumeInputSchema,
745
- anchoredDeletePolicySchema: () => anchoredDeletePolicySchema,
746
- anchoredReplacePolicySchema: () => anchoredReplacePolicySchema,
747
- changeSpeechScriptInputSchema: () => changeSpeechScriptInputSchema,
748
- changeSpeechVoiceInputSchema: () => changeSpeechVoiceInputSchema,
749
- clipIdSchema: () => clipIdSchema,
750
- clipIdsSchema: () => clipIdsSchema,
751
- deleteBgmInputSchema: () => deleteBgmInputSchema,
752
- deleteSpeechesInputSchema: () => deleteSpeechesInputSchema,
753
- deleteVideoClipsInputSchema: () => deleteVideoClipsInputSchema,
754
- mediaIdSchema: () => mediaIdSchema,
755
- moveAnchorSchema: () => moveAnchorSchema,
756
- moveSpeechesInputSchema: () => moveSpeechesInputSchema,
757
- moveVideoClipsByAnchorInputSchema: () => moveVideoClipsByAnchorInputSchema,
758
- moveVideoClipsInputSchema: () => moveVideoClipsInputSchema,
759
- movedClipAnchoredPolicySchema: () => movedClipAnchoredPolicySchema,
760
- positiveMsSchema: () => positiveMsSchema,
761
- replaceVideoClipContentInputSchema: () => replaceVideoClipContentInputSchema,
762
- replaceVideoClipSequenceInputSchema: () => replaceVideoClipSequenceInputSchema,
763
- setBgmInputSchema: () => setBgmInputSchema,
764
- setCaptionStyleInputSchema: () => setCaptionStyleInputSchema,
765
- setCaptionVisibilityInputSchema: () => setCaptionVisibilityInputSchema,
766
- setVideoClipSpeedShiftInputSchema: () => setVideoClipSpeedShiftInputSchema,
767
- speechAssetsSchema: () => speechAssetsSchema,
768
- speechIdSchema: () => speechIdSchema,
769
- speechIdsSchema: () => speechIdsSchema,
770
- speedShiftSchema: () => speedShiftSchema,
771
- timelineMsSchema: () => timelineMsSchema,
772
- voiceSchema: () => voiceSchema,
773
- volumeSchema: () => volumeSchema
774
- });
775
- //#endregion
776
- //#region src/editor/snapshot-utils.ts
777
- function isMap(value) {
778
- return value instanceof Map;
779
- }
780
- function snapshotToPlain(value) {
781
- if (value instanceof Map) {
782
- const obj = {};
783
- for (const [k, v] of value) obj[String(k)] = snapshotToPlain(v);
784
- return obj;
785
- }
786
- if (Array.isArray(value)) return value.map(snapshotToPlain);
787
- return value;
788
- }
789
- function getAt(snapshot, ...keys) {
790
- let cur = snapshot;
791
- for (const k of keys) if (cur instanceof Map) cur = cur.get(k);
792
- else if (cur != null && typeof cur === "object") cur = cur[k];
793
- else return;
794
- return cur;
795
- }
796
- function readMainTrackItems(snapshot) {
797
- const tracks = getAt(snapshot, "tracks");
798
- const items = getAt(Array.isArray(tracks) ? tracks.find((t) => getAt(t, "parts_kind") === "video_clip") : void 0, "items");
799
- if (!Array.isArray(items)) return [];
800
- return items.map((item) => {
801
- if (item instanceof Map) return { part_id: optionalString(item.get("part_id")) };
802
- if (item != null && typeof item === "object") return { part_id: optionalString(item.part_id) };
803
- return { part_id: void 0 };
804
- });
805
- }
806
- /**
807
- * The effective timeline duration of a part read from a raw snapshot. No part
808
- * stores a derived `duration_ms` in authoritative state (reference/17 §5): a
809
- * video clip's length is its trim window `play_out - play_in` over the speed
810
- * multiplier; a speech's is its intrinsic `media_duration_ms`; a caption's is its
811
- * `initial_duration_ms`. Falls back to `fallback` when no source value is
812
- * resolvable (e.g. an unknown/raw part).
813
- */
814
- function readPartDurationMs(snapshot, partId, fallback = 1e3) {
815
- const part = readPart(snapshot, partId);
816
- if (part == null) return fallback;
817
- let sourceMs;
818
- if (part.part_kind === "video_clip") {
819
- const playIn = numericValue(part.play_in);
820
- const playOut = numericValue(part.play_out);
821
- if (playIn != null && playOut != null) {
822
- const speed = videoClipSpeed(part.speed_shift);
823
- sourceMs = Math.round((playOut - playIn) / speed);
824
- }
825
- } else if (part.part_kind === "speech") sourceMs = numericValue(part.media_duration_ms);
826
- else if (part.part_kind === "caption") sourceMs = numericValue(part.initial_duration_ms);
827
- return sourceMs != null && Number.isFinite(sourceMs) ? sourceMs : fallback;
828
- }
829
- function numericValue(value) {
830
- return typeof value === "number" && Number.isFinite(value) ? value : void 0;
831
- }
832
- /** The linear speed multiplier of a raw `speed_shift` blob, defaulting to 1. */
833
- function videoClipSpeed(speedShift) {
834
- const speed = speedShift?.config?.linear?.speed;
835
- return typeof speed === "number" && Number.isFinite(speed) && speed > 0 ? speed : 1;
836
- }
837
- function readPart(snapshot, partId) {
838
- const part = getAt(snapshot, "part_library", partId);
839
- if (part == null) return null;
840
- const plain = snapshotToPlain(part);
841
- if ("video_clip" in plain && plain.video_clip != null) return {
842
- ...plain.video_clip,
843
- part_kind: "video_clip"
844
- };
845
- if ("speech" in plain && plain.speech != null) return {
846
- ...plain.speech,
847
- part_kind: "speech"
848
- };
849
- if ("caption" in plain && plain.caption != null) return {
850
- ...plain.caption,
851
- part_kind: "caption"
852
- };
853
- if ("bgm" in plain && plain.bgm != null) return {
854
- ...plain.bgm,
855
- part_kind: "bgm"
856
- };
857
- return plain;
858
- }
859
- function optionalString(value) {
860
- return typeof value === "string" ? value : void 0;
861
- }
862
- //#endregion
863
- //#region src/editor/schema-validator.ts
864
- var ValidationError = class extends Error {
865
- code;
866
- context;
867
- constructor(code, message, context) {
868
- super(`[${code}] ${message}`);
869
- this.name = "ValidationError";
870
- this.code = code;
871
- this.context = context;
872
- }
873
- };
874
- var SchemaValidator = class {
875
- validateMoveVideoClips(input, doc) {
876
- const parsed = this.parse(moveVideoClipsInputSchema, input, "move_invalid_input");
877
- const knownIds = this.mainTrackIds(doc);
878
- 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 });
879
- }
880
- /**
881
- * The anchor must not be one of the clips being moved. Unlike the schema's
882
- * mutual exclusions this needs the input read as a whole, so it stays here.
883
- *
884
- * Rejected rather than normalized: "put this block before itself" has no
885
- * defensible outcome — treating it as a no-op hides a caller bug behind a
886
- * success, and picking any surviving neighbour invents an intent. The legacy
887
- * `applyBatchMoveVideoClips` omits this check (a self-anchor there silently
888
- * lands the block at its own pre-move index); director's original move tool
889
- * did enforce it (`_validate_position`: "不是被移动的clips"), and that is the
890
- * behaviour worth keeping.
891
- */
892
- validateMoveVideoClipsByAnchor(input, doc) {
893
- const parsed = this.parse(moveVideoClipsByAnchorInputSchema, input, "move_by_anchor_invalid_input");
894
- const knownIds = this.mainTrackIds(doc);
895
- for (const clipId of parsed.clip_ids) if (!knownIds.has(clipId)) throw new ValidationError("move_by_anchor_clip_not_found", `clip_id "${clipId}" not present on main_track`, { clip_id: clipId });
896
- if (parsed.anchor.position === "track_start") return;
897
- const anchorId = parsed.anchor.clip_id;
898
- if (!knownIds.has(anchorId)) throw new ValidationError("move_by_anchor_anchor_not_found", `anchor clip_id "${anchorId}" not present on main_track`, { anchor_clip_id: anchorId });
899
- if (parsed.clip_ids.includes(anchorId)) throw new ValidationError("move_by_anchor_anchor_is_moved", `anchor clip_id "${anchorId}" is itself being moved`, { anchor_clip_id: anchorId });
900
- }
901
- validateDeleteVideoClips(input, doc) {
902
- const parsed = this.parse(deleteVideoClipsInputSchema, input, "delete_invalid_input");
903
- const knownIds = this.mainTrackIds(doc);
904
- 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 });
905
- }
906
- validateAddVideoClips(input, doc) {
907
- const parsed = this.parse(addVideoClipsInputSchema, input, "add_invalid_input");
908
- const knownIds = this.mainTrackIds(doc);
909
- 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 });
910
- 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 });
911
- }
912
- validateAdjustVideoClipVolume(input, doc) {
913
- const parsed = this.parse(adjustVideoClipVolumeInputSchema, input, "adjust_volume_invalid_input");
914
- const snapshot = doc.snapshot();
915
- for (const c of parsed.clips) this.assertPartKind(snapshot, c.clip_id, "video_clip", "adjust_volume");
916
- }
917
- validateSetVideoClipSpeedShift(input, doc) {
918
- const parsed = this.parse(setVideoClipSpeedShiftInputSchema, input, "set_speed_invalid_input");
919
- const snapshot = doc.snapshot();
920
- for (const c of parsed.clips) this.assertPartKind(snapshot, c.clip_id, "video_clip", "set_speed");
921
- }
922
- validateReplaceVideoClipContent(input, doc) {
923
- const parsed = this.parse(replaceVideoClipContentInputSchema, input, "replace_content_invalid_input");
924
- const snapshot = doc.snapshot();
925
- for (const c of parsed.clips) {
926
- this.assertPartKind(snapshot, c.clip_id, "video_clip", "replace_content");
927
- 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}`, {
928
- clip_id: c.clip_id,
929
- play_in: c.play_in,
930
- play_out: c.play_out,
931
- media_duration_ms: c.media_duration_ms
932
- });
933
- }
934
- }
935
- /**
936
- * `old_clip_ids` must name a **contiguous run of the main track, in track
937
- * order**. A sparse or reordered selection is rejected rather than normalized:
938
- * a sparse selection has no single stretch to swap, so the insert index, the
939
- * positional speech remap, and the resulting order would each need a different
940
- * arbitrary choice. Mirrors the agent tool's own contract ("must be a
941
- * contiguous main-track sequence listed in timeline order").
942
- */
943
- validateReplaceVideoClipSequence(input, doc) {
944
- const parsed = this.parse(replaceVideoClipSequenceInputSchema, input, "replace_sequence_invalid_input");
945
- const items = readMainTrackItems(doc.snapshot());
946
- const indexById = new Map(items.map((it, index) => [it.part_id, index]));
947
- const indices = [];
948
- for (const clipId of parsed.old_clip_ids) {
949
- const index = indexById.get(clipId);
950
- if (index == null) throw new ValidationError("replace_sequence_clip_not_found", `clip_id "${clipId}" not present on main_track`, { clip_id: clipId });
951
- indices.push(index);
952
- }
953
- for (let i = 1; i < indices.length; i++) if (indices[i] !== (indices[i - 1] ?? 0) + 1) throw new ValidationError("replace_sequence_not_contiguous", `old_clip_ids must be a contiguous main-track run in track order, got indices [${indices.join(", ")}]`, { indices });
954
- }
955
- validateAdjustVideoClipDuration(input, doc) {
956
- const parsed = this.parse(adjustVideoClipDurationInputSchema, input, "adjust_duration_invalid_input");
957
- const snapshot = doc.snapshot();
958
- for (const c of parsed.clips) {
959
- this.assertPartKind(snapshot, c.clip_id, "video_clip", "adjust_duration");
960
- if (c.play_out <= c.play_in) throw new ValidationError("adjust_duration_invalid_window", `play_out must be greater than play_in`, {
961
- clip_id: c.clip_id,
962
- play_in: c.play_in,
963
- play_out: c.play_out
964
- });
965
- }
966
- }
967
- validateAdjustSpeechVolume(input, doc) {
968
- const parsed = this.parse(adjustSpeechVolumeInputSchema, input, "adjust_speech_volume_invalid_input");
969
- const snapshot = doc.snapshot();
970
- for (const s of parsed.speeches) this.assertPartKind(snapshot, s.speech_id, "speech", "adjust_speech_volume");
971
- }
972
- validateAdjustBgmVolume(input, doc) {
973
- const parsed = this.parse(adjustBgmVolumeInputSchema, input, "adjust_bgm_volume_invalid_input");
974
- const snapshot = doc.snapshot();
975
- for (const b of parsed.bgm) this.assertPartKind(snapshot, b.bgm_id, "bgm", "adjust_bgm_volume");
976
- }
977
- validateAddSpeeches(input, doc) {
978
- const parsed = this.parse(addSpeechesInputSchema, input, "add_speeches_invalid_input");
979
- this.assertCaptionsOwned(parsed, "add_speeches");
980
- this.assertAnchorsExist(parsed, doc, "add_speeches");
981
- }
982
- validateChangeSpeechScript(input, doc) {
983
- const parsed = this.parse(changeSpeechScriptInputSchema, input, "change_script_invalid_input");
984
- this.assertCaptionsOwned(parsed, "change_script");
985
- this.assertSpeechesExist(parsed, doc, "change_script");
986
- this.assertAnchorsExist(parsed, doc, "change_script");
987
- }
988
- validateChangeSpeechVoice(input, doc) {
989
- const parsed = this.parse(changeSpeechVoiceInputSchema, input, "change_voice_invalid_input");
990
- this.assertCaptionsOwned(parsed, "change_voice");
991
- this.assertSpeechesExist(parsed, doc, "change_voice");
992
- this.assertAnchorsExist(parsed, doc, "change_voice");
993
- }
994
- validateDeleteSpeeches(input, doc) {
995
- const parsed = this.parse(deleteSpeechesInputSchema, input, "delete_speeches_invalid_input");
996
- const snapshot = doc.snapshot();
997
- for (const id of parsed.speech_ids) this.assertPartKind(snapshot, id, "speech", "delete_speeches");
998
- }
999
- validateMoveSpeeches(input, doc) {
1000
- const parsed = this.parse(moveSpeechesInputSchema, input, "move_speeches_invalid_input");
1001
- const snapshot = doc.snapshot();
1002
- for (const s of parsed.speeches) this.assertPartKind(snapshot, s.speech_id, "speech", "move_speeches");
1003
- }
1004
- validateSetBgm(input, _doc) {
1005
- this.parse(setBgmInputSchema, input, "set_bgm_invalid_input");
1006
- }
1007
- validateDeleteBgm(input, _doc) {
1008
- this.parse(deleteBgmInputSchema, input, "delete_bgm_invalid_input");
1009
- }
1010
- validateSetCaptionVisibility(input, _doc) {
1011
- this.parse(setCaptionVisibilityInputSchema, input, "set_caption_visibility_invalid_input");
1012
- }
1013
- validateSetCaptionStyle(input, _doc) {
1014
- this.parse(setCaptionStyleInputSchema, input, "set_caption_style_invalid_input");
1015
- }
1016
- parse(schema, input, code) {
1017
- const parsed = schema.safeParse(input);
1018
- if (!parsed.success) throw new ValidationError(code, parsed.error.message, { issues: parsed.error.issues });
1019
- return parsed.data;
1020
- }
1021
- mainTrackIds(doc) {
1022
- const items = readMainTrackItems(doc.snapshot());
1023
- return new Set(items.map((it) => it.part_id).filter((id) => id != null));
1024
- }
1025
- /** Every caption a speech declares in `caption_ids` must be supplied in `captions`. */
1026
- assertCaptionsOwned(assets, opName) {
1027
- const supplied = new Set(assets.captions.map((c) => c.caption_id));
1028
- 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`, {
1029
- speech_id: speech.speech_id,
1030
- caption_id: captionId
1031
- });
1032
- }
1033
- /** Each regenerated speech (re-TTS) must already exist in the document. */
1034
- assertSpeechesExist(assets, doc, opName) {
1035
- const snapshot = doc.snapshot();
1036
- for (const speech of assets.speeches) this.assertPartKind(snapshot, speech.speech_id, "speech", opName);
1037
- }
1038
- /**
1039
- * Each speech's `anchor_part_id` must point at a video clip that already
1040
- * exists. A relative speech anchored to a missing clip cannot be positioned
1041
- * by the projection and has no host to recover to (RFC 02 §4/§11.1), so the
1042
- * write must be rejected rather than landing a dangling reference. (Restores
1043
- * the dangling-reference guard the old `validateMaterializedPatch` carried;
1044
- * ADR 0009.)
1045
- */
1046
- assertAnchorsExist(assets, doc, opName) {
1047
- const snapshot = doc.snapshot();
1048
- for (const speech of assets.speeches) this.assertPartKind(snapshot, speech.anchor_part_id, "video_clip", `${opName}_anchor`);
1049
- }
1050
- assertPartKind(snapshot, partId, kind, opName) {
1051
- const part = readPart(snapshot, partId);
1052
- if (part == null) throw new ValidationError(`${opName}_part_not_found`, `part_library has no entry for "${partId}"`, { part_id: partId });
1053
- if (part.part_kind !== kind) throw new ValidationError(`${opName}_wrong_part_kind`, `part_id "${partId}" is kind "${String(part.part_kind)}", expected "${kind}"`, {
1054
- part_id: partId,
1055
- part_kind: part.part_kind
1056
- });
1057
- }
1058
- };
1059
- //#endregion
1060
- //#region src/timeline-core/locate.ts
1061
- /**
1062
- * The flow-ordered main-track clip ranges (cumulative effective durations from
1063
- * 0). Empty-media gap fillers are not in authoritative state, so this reflects
1064
- * only the real clips the draft stores.
1065
- */
1066
- function mainTrackRanges(draft) {
1067
- const partLibrary = readPartLibrary(draft);
1068
- const items = (draft.tracks ?? []).find((t) => t?.parts_kind === "video_clip")?.items ?? [];
1069
- const ranges = [];
1070
- let cursor = 0;
1071
- for (const item of items) {
1072
- const partId = item?.part_id;
1073
- if (partId == null) continue;
1074
- const clip = partLibrary[partId]?.video_clip;
1075
- if (clip == null) continue;
1076
- const durationMs = effectiveVideoClipDurationMs(clip);
1077
- ranges.push({
1078
- partId,
1079
- startMs: cursor,
1080
- endMs: cursor + durationMs
1081
- });
1082
- cursor += durationMs;
1083
- }
1084
- return ranges;
1085
- }
1086
- /**
1087
- * Pick the host video clip an absolute time lands in, with the harness two-sided
1088
- * fallback: before the first clip → first clip; after the last → last clip.
1089
- * Returns null only when there is no clip at all (caller leaves the item as-is).
1090
- */
1091
- function hostForAbsMs(ranges, absMs) {
1092
- if (ranges.length === 0) return null;
1093
- const hit = ranges.find((r) => r.startMs <= absMs && absMs < r.endMs);
1094
- if (hit != null) return hit;
1095
- return absMs < ranges[0].startMs ? ranges[0] : ranges[ranges.length - 1];
1096
- }
1097
- /**
1098
- * Build an `anchored` time position anchoring `absMs` to the host clip it lands
1099
- * in (offset clamped to a non-negative integer). Falls back to `absolute` when
1100
- * there is no host clip. Pair with `fallbackAbsMs = absMs` on the item.
1101
- */
1102
- function relativePositionForAbs(ranges, absMs) {
1103
- const host = hostForAbsMs(ranges, absMs);
1104
- if (host == null) return {
1105
- mode: "absolute",
1106
- offsetMs: Math.round(absMs)
1107
- };
1108
- return {
1109
- mode: "anchored",
1110
- anchorPartId: host.partId,
1111
- offsetMs: Math.max(0, Math.round(absMs - host.startMs))
1112
- };
1113
- }
1114
- function readPartLibrary(draft) {
1115
- const out = {};
1116
- for (const [partId, part] of recordEntries(draft.part_library)) if (part != null) out[partId] = part;
1117
- return out;
1118
- }
1119
- //#endregion
1120
- //#region src/editor/semantic-editor.ts
1121
- var SemanticEditor = class {
1122
- doc;
1123
- validator;
1124
- constructor(doc, validator = new SchemaValidator()) {
1125
- this.doc = doc;
1126
- this.validator = validator;
1127
- }
1128
- async moveVideoClips(input, options) {
1129
- this.validator.validateMoveVideoClips(input, this.doc);
1130
- this.doc.transact((draft) => {
1131
- const items = mainTrackItems(draft);
1132
- if (items == null) return;
1133
- const beforeRanges = mainTrackRanges(draft);
1134
- for (const clip of input.clips) {
1135
- const layout = this.computeMainTrackLayout(draft);
1136
- const fromItem = layout.find((it) => it.part_id === clip.clip_id);
1137
- if (fromItem == null) throw new Error(`moveVideoClips: clip "${clip.clip_id}" disappeared mid-batch`);
1138
- const toIndex = this.indexForStartMs(layout, clip.new_start_ms, fromItem.index);
1139
- if (toIndex === fromItem.index) continue;
1140
- moveItem(items, fromItem.index, toIndex);
1141
- }
1142
- reparentSpeechesAfterMainTrackChange(draft, beforeRanges);
1143
- }, audit("MoveVideoClips", input, options));
1144
- }
1145
- /**
1146
- * Reorder main-track clips relative to an anchor clip, moving them as one block.
1147
- *
1148
- * The ordinal sibling of `moveVideoClips`: main-track clips are
1149
- * `sequential`-positioned, so their order is the authoritative fact and absolute
1150
- * time is derived on read (RFC 02 §4/§7). A caller that already knows "put these
1151
- * after that one" should say so, instead of computing a timeline offset that this
1152
- * editor would only have to resolve back into an index.
1153
- *
1154
- * `on_anchored` decides the speech treatment and is required. Note the asymmetry
1155
- * in cost: `follow` writes nothing to the speeches (their `{ anchorPartId,
1156
- * offsetMs }` facts stay valid and the derived absolute time moves with the host),
1157
- * while `keep_absolute` runs the extra re-parent pass that preserves each
1158
- * speech's absolute landing. `moveVideoClips` is permanently `keep_absolute`,
1159
- * matching the FE timeline it serves.
1160
- */
1161
- async moveVideoClipsByAnchor(input, options) {
1162
- this.validator.validateMoveVideoClipsByAnchor(input, this.doc);
1163
- this.doc.transact((draft) => {
1164
- const items = mainTrackItems(draft);
1165
- if (items == null) return;
1166
- const beforeRanges = mainTrackRanges(draft);
1167
- const targets = new Set(input.clip_ids);
1168
- const movedIndices = [];
1169
- for (let i = 0; i < items.length; i++) {
1170
- const partId = items[i]?.part_id;
1171
- if (partId != null && targets.has(partId)) movedIndices.push(i);
1172
- }
1173
- if (movedIndices.length === 0) return;
1174
- const insertIndex = anchorInsertIndex(items, input.anchor);
1175
- const moved = movedIndices.map((index) => items[index]);
1176
- for (let i = movedIndices.length - 1; i >= 0; i--) items.splice(movedIndices[i], 1);
1177
- const removedBefore = movedIndices.filter((index) => index < insertIndex).length;
1178
- items.splice(insertIndex - removedBefore, 0, ...moved);
1179
- if (input.on_anchored === "keep_absolute") reparentSpeechesAfterMainTrackChange(draft, beforeRanges);
1180
- else refreshAnchoredFallbackAbs(draft);
1181
- }, audit("MoveVideoClipsByAnchor", input, options));
1182
- }
1183
- async deleteVideoClips(input, options) {
1184
- this.validator.validateDeleteVideoClips(input, this.doc);
1185
- const targets = new Set(input.clip_ids);
1186
- const policy = input.on_anchored ?? "cascade";
1187
- this.doc.transact((draft) => {
1188
- const track = mainTrackRow(draft);
1189
- if (track?.items == null) return;
1190
- if (policy === "detach") for (const clipId of targets) detachAnchoredChildren(draft, clipId);
1191
- track.items = track.items.filter((item) => item.part_id == null || !targets.has(item.part_id));
1192
- for (const clipId of targets) {
1193
- if (policy === "cascade") deleteAnchoredSubtree(draft, clipId);
1194
- deletePart(draft, clipId);
1195
- }
1196
- }, audit("DeleteVideoClips", input, options));
1197
- }
1198
- async addVideoClips(input, options) {
1199
- this.validator.validateAddVideoClips(input, this.doc);
1200
- const insertIndex = this.computeAddInsertIndex(input);
1201
- this.doc.transact((draft) => {
1202
- const track = ensureMainTrack(draft);
1203
- track.items ??= [];
1204
- let at = insertIndex;
1205
- for (const clip of input.clips) {
1206
- const partId = generatePartId("clip");
1207
- setPart(draft, partId, { video_clip: {
1208
- id: partId,
1209
- kind: "video_clip",
1210
- play_in: clip.play_in ?? 0,
1211
- play_out: clip.play_out ?? clip.media_duration_ms,
1212
- volume: 0,
1213
- origin_media_id: clip.media_id
1214
- } });
1215
- track.items.splice(at, 0, {
1216
- part_id: partId,
1217
- time_position: { mode: "sequential" },
1218
- fallback_abs_ms: void 0
1219
- });
1220
- at += 1;
1221
- }
1222
- }, audit("AddVideoClips", input, options));
1223
- }
1224
- /**
1225
- * Swap a contiguous run of main-track clips for a new run, in one transaction.
1226
- *
1227
- * Composite by necessity, not convenience: with `on_anchored: 'remap'` each
1228
- * surviving speech is re-anchored to the new clip in the **same position of the
1229
- * sequence** (old[i] → new[i]), which needs both id sets live at once. Splitting
1230
- * it into `deleteVideoClips` + `addVideoClips` would leave the caller holding the
1231
- * anchor re-wiring — the cascade-in-the-caller mistake ADR 0009 retires.
1232
- *
1233
- * Positional pairing, not by count: when there are fewer new clips than old, the
1234
- * unmatched old clips have no counterpart, so their anchored subtrees are deleted
1235
- * (there is nothing to anchor to). When there are more new clips than old, the
1236
- * extra ones simply arrive with no children.
1237
- *
1238
- * The new clips are inserted where the run started, so surrounding order is
1239
- * preserved. Every position is derived on read (RFC 02 §7) — this writes only the
1240
- * facts: track order, the trim windows, and the surviving anchors.
1241
- */
1242
- async replaceVideoClipSequence(input, options) {
1243
- this.validator.validateReplaceVideoClipSequence(input, this.doc);
1244
- this.doc.transact((draft) => {
1245
- const track = mainTrackRow(draft);
1246
- if (track?.items == null) return;
1247
- const targets = new Set(input.old_clip_ids);
1248
- const insertIndex = track.items.findIndex((item) => item?.part_id != null && targets.has(item.part_id));
1249
- const at = insertIndex < 0 ? track.items.length : insertIndex;
1250
- const newItems = [];
1251
- const newClipIds = [];
1252
- for (const clip of input.new_clips) {
1253
- const partId = generatePartId("clip");
1254
- setPart(draft, partId, { video_clip: {
1255
- id: partId,
1256
- kind: "video_clip",
1257
- play_in: clip.play_in ?? 0,
1258
- play_out: clip.play_out ?? clip.media_duration_ms,
1259
- volume: 0,
1260
- origin_media_id: clip.media_id ?? ""
1261
- } });
1262
- newItems.push({
1263
- part_id: partId,
1264
- time_position: { mode: "sequential" },
1265
- fallback_abs_ms: void 0
1266
- });
1267
- newClipIds.push(partId);
1268
- }
1269
- if (input.on_anchored === "remap") for (let i = 0; i < input.old_clip_ids.length; i++) {
1270
- const newClipId = newClipIds[i];
1271
- if (newClipId == null) {
1272
- deleteAnchoredSubtree(draft, input.old_clip_ids[i]);
1273
- continue;
1274
- }
1275
- reanchorAnchoredChildren(draft, input.old_clip_ids[i], newClipId);
1276
- }
1277
- else for (const clipId of targets) deleteAnchoredSubtree(draft, clipId);
1278
- track.items = track.items.filter((item) => item?.part_id == null || !targets.has(item.part_id));
1279
- for (const clipId of targets) deletePart(draft, clipId);
1280
- track.items.splice(at, 0, ...newItems);
1281
- }, audit("ReplaceVideoClipSequence", input, options));
1282
- }
1283
- async adjustVideoClipVolume(input, options) {
1284
- this.validator.validateAdjustVideoClipVolume(input, this.doc);
1285
- this.doc.transact((draft) => {
1286
- for (const c of input.clips) {
1287
- const videoClip = draft.part_library?.[c.clip_id]?.video_clip;
1288
- if (videoClip == null) continue;
1289
- setPart(draft, c.clip_id, { video_clip: {
1290
- ...videoClip,
1291
- kind: "video_clip",
1292
- volume: c.volume
1293
- } });
1294
- }
1295
- }, audit("AdjustVideoClipVolume", input, options));
1296
- }
1297
- async setVideoClipSpeedShift(input, options) {
1298
- this.validator.validateSetVideoClipSpeedShift(input, this.doc);
1299
- this.doc.transact((draft) => {
1300
- for (const clip of input.clips) {
1301
- const videoClip = draft.part_library?.[clip.clip_id]?.video_clip;
1302
- if (videoClip == null) continue;
1303
- const next = {
1304
- ...videoClip,
1305
- kind: "video_clip",
1306
- speed_shift: normalizeSpeedShift(clip.speed_shift)
1307
- };
1308
- setPart(draft, clip.clip_id, { video_clip: next });
1309
- }
1310
- }, audit("SetVideoClipSpeedShift", input, options));
1311
- }
1312
- async adjustSpeechVolume(input, options) {
1313
- this.validator.validateAdjustSpeechVolume(input, this.doc);
1314
- this.doc.transact((draft) => {
1315
- for (const s of input.speeches) {
1316
- const speech = draft.part_library?.[s.speech_id]?.speech;
1317
- if (speech == null) continue;
1318
- setPart(draft, s.speech_id, { speech: {
1319
- ...speech,
1320
- kind: "speech",
1321
- volume: s.volume
1322
- } });
1323
- }
1324
- }, audit("AdjustSpeechVolume", input, options));
1325
- }
1326
- async adjustBgmVolume(input, options) {
1327
- this.validator.validateAdjustBgmVolume(input, this.doc);
1328
- this.doc.transact((draft) => {
1329
- for (const b of input.bgm) {
1330
- const bgm = draft.part_library?.[b.bgm_id]?.bgm;
1331
- if (bgm == null) continue;
1332
- setPart(draft, b.bgm_id, { bgm: {
1333
- ...bgm,
1334
- kind: "bgm",
1335
- volume: b.volume
1336
- } });
1337
- }
1338
- }, audit("AdjustBgmVolume", input, options));
1339
- }
1340
- /**
1341
- * Replace the media backing existing video clips. The new media's stable
1342
- * result (new media id, intrinsic length, reset trim window) is materialized
1343
- * upstream; the clip ids and their track items are unchanged. Only the part
1344
- * facts change — effective duration and downstream positions are derived by
1345
- * the projection on read.
1346
- */
1347
- async replaceVideoClipContent(input, options) {
1348
- this.validator.validateReplaceVideoClipContent(input, this.doc);
1349
- this.doc.transact((draft) => {
1350
- for (const clip of input.clips) {
1351
- const videoClip = draft.part_library?.[clip.clip_id]?.video_clip;
1352
- if (videoClip == null) continue;
1353
- setPart(draft, clip.clip_id, { video_clip: {
1354
- ...videoClip,
1355
- kind: "video_clip",
1356
- origin_media_id: clip.origin_media_id,
1357
- play_in: clip.play_in,
1358
- play_out: clip.play_out,
1359
- volume: clip.volume,
1360
- speed_shift: void 0
1361
- } });
1362
- }
1363
- }, audit("ReplaceVideoClipContent", input, options));
1364
- }
1365
- /**
1366
- * Re-trim clips. Only the `play_in` / `play_out` facts change; the new
1367
- * effective duration and the resulting downstream reflow are derived by the
1368
- * projection on read (anchored speeches follow their host clip automatically).
1369
- */
1370
- async adjustVideoClipDuration(input, options) {
1371
- this.validator.validateAdjustVideoClipDuration(input, this.doc);
1372
- this.doc.transact((draft) => {
1373
- for (const clip of input.clips) {
1374
- const videoClip = draft.part_library?.[clip.clip_id]?.video_clip;
1375
- if (videoClip == null) continue;
1376
- setPart(draft, clip.clip_id, { video_clip: {
1377
- ...videoClip,
1378
- kind: "video_clip",
1379
- play_in: clip.play_in,
1380
- play_out: clip.play_out
1381
- } });
1382
- }
1383
- refreshAnchoredFallbackAbs(draft);
1384
- }, audit("AdjustVideoClipDuration", input, options));
1385
- }
1386
- /**
1387
- * Add speeches (and their captions). TTS runs upstream; the materialized
1388
- * speech / caption parts arrive in `input`, each carrying its host clip
1389
- * `anchor_part_id` + `offset_ms`. The editor writes the parts and their
1390
- * `anchored` `time_position` facts verbatim — no write-time host-picking, no
1391
- * cascade. The projection derives absolute positions on read.
1392
- */
1393
- async addSpeeches(input, options) {
1394
- this.validator.validateAddSpeeches(input, this.doc);
1395
- this.doc.transact((draft) => {
1396
- writeSpeechAssets(draft, input);
1397
- }, audit("AddSpeeches", input, options));
1398
- }
1399
- /** Delete speeches with their captions; the subtree is removed (§9.2). Surviving lanes keep their facts. */
1400
- async deleteSpeeches(input, options) {
1401
- this.validator.validateDeleteSpeeches(input, this.doc);
1402
- this.doc.transact((draft) => {
1403
- for (const speechId of input.speech_ids) deleteSpeechSubtree(draft, speechId);
1404
- }, audit("DeleteSpeeches", input, options));
1405
- }
1406
- /**
1407
- * Move speeches in time (§9.1). One forward positioning pass: anchor each
1408
- * speech to the main-track clip its new absolute start lands in and write the
1409
- * resulting `anchored` `time_position` fact. No cascade — the projection
1410
- * derives absolute positions on read.
1411
- */
1412
- async moveSpeeches(input, options) {
1413
- this.validator.validateMoveSpeeches(input, this.doc);
1414
- this.doc.transact((draft) => {
1415
- const ranges = mainTrackRanges(draft);
1416
- const track = findLaneTrack(draft, "speech");
1417
- for (const move of input.speeches) {
1418
- const item = (track?.items ?? []).find((it) => it?.part_id === move.speech_id);
1419
- if (item == null) continue;
1420
- item.time_position = relativePositionForAbs(ranges, move.new_start_ms);
1421
- item.fallback_abs_ms = move.new_start_ms;
1422
- }
1423
- }, audit("MoveSpeeches", input, options));
1424
- }
1425
- /** Change a speech's script. Re-TTS runs upstream; the regenerated parts arrive materialized (no cascade). */
1426
- async changeSpeechScript(input, options) {
1427
- this.validator.validateChangeSpeechScript(input, this.doc);
1428
- this.doc.transact((draft) => {
1429
- writeSpeechAssets(draft, input);
1430
- }, audit("ChangeSpeechScript", input, options));
1431
- }
1432
- /** Change a speech's voice. Re-TTS runs upstream; the regenerated parts arrive materialized (no cascade). */
1433
- async changeSpeechVoice(input, options) {
1434
- this.validator.validateChangeSpeechVoice(input, this.doc);
1435
- this.doc.transact((draft) => {
1436
- writeSpeechAssets(draft, input);
1437
- }, audit("ChangeSpeechVoice", input, options));
1438
- }
1439
- /**
1440
- * Set the document BGM. The media's stable result arrives materialized.
1441
- *
1442
- * KNOWN GAP (non-blocking, FE callers only): a public-library BGM also needs
1443
- * project-level media ownership registered, or it plays but never appears in
1444
- * the project's media library. This editor deliberately does not do it — the
1445
- * document edit is complete and correct, and ownership is a side effect owned
1446
- * by whoever holds the authoritative media data (memota), not by a CRDT write.
1447
- *
1448
- * Who is affected, as of 2026-08-13: the agent harness registers it on BOTH
1449
- * its legacy and mengine paths (shared `resolveMutation` calls memota
1450
- * `attachMedia` directly), and FE's legacy REST path gets it from Director.
1451
- * Only FE's mengine path is missing it. FE cannot call memota directly:
1452
- * `attachMedia` is exposed on memota's internal contract only, so the fix is a
1453
- * Director proxy route — NOT a new Director business endpoint, since Director
1454
- * stopped owning media ownership entirely (`a2951355`, 2026-08-05).
1455
- *
1456
- * Tracked as a Phase 7 gate (it must land before "new documents default to
1457
- * mengine" makes public-library BGM a routine operation). See
1458
- * `docs/projects/medeo-integration/results/phase-6-m4-legacy-refresh-isolation.md`.
1459
- */
1460
- async setBgm(input, options) {
1461
- this.validator.validateSetBgm(input, this.doc);
1462
- this.doc.transact((draft) => {
1463
- const track = ensureLaneTrack(draft, "bgm");
1464
- for (const item of track.items ?? []) if (item?.part_id != null) deletePart(draft, item.part_id);
1465
- setPart(draft, input.bgm_id, { bgm: {
1466
- id: input.bgm_id,
1467
- kind: "bgm",
1468
- audio_storage_key: input.audio_storage_key,
1469
- volume: input.volume,
1470
- origin_media_id: input.origin_media_id
1471
- } });
1472
- track.items = [{
1473
- part_id: input.bgm_id,
1474
- time_position: {
1475
- mode: "absolute",
1476
- offsetMs: 0
1477
- },
1478
- fallback_abs_ms: void 0
1479
- }];
1480
- }, audit("SetBgm", input, options));
1481
- }
1482
- /** Remove the document BGM; clears the bgm lane and removes the part. */
1483
- async deleteBgm(input, options) {
1484
- this.validator.validateDeleteBgm(input, this.doc);
1485
- this.doc.transact((draft) => {
1486
- const track = findLaneTrack(draft, "bgm");
1487
- if (track == null) return;
1488
- for (const item of track.items ?? []) if (item?.part_id != null) deletePart(draft, item.part_id);
1489
- track.items = [];
1490
- }, audit("DeleteBgm", input, options));
1491
- }
1492
- /** Toggle caption visibility (caption track `is_hidden`). */
1493
- async setCaptionVisibility(input, options) {
1494
- this.validator.validateSetCaptionVisibility(input, this.doc);
1495
- this.doc.transact((draft) => {
1496
- const track = findLaneTrack(draft, "caption");
1497
- if (track == null) return;
1498
- track.is_hidden = input.is_hidden;
1499
- }, audit("SetCaptionVisibility", input, options));
1500
- }
1501
- /**
1502
- * Set the document-wide caption style. GLOBAL (no `caption_id`): the patch is
1503
- * merged onto EVERY caption part's `style`, mirroring the FE, which applies a
1504
- * single style to all captions (`caption-style.ts:persistCaptionStylePatch`).
1505
- *
1506
- * Merge, not replace: only the fields present in the input overwrite the
1507
- * caption's existing style; absent fields are carried forward. So a partial
1508
- * patch ("recolor only") keeps the caption's font size. Pure document edit, no
1509
- * cascade — positions are untouched.
1510
- */
1511
- async setCaptionStyle(input, options) {
1512
- this.validator.validateSetCaptionStyle(input, this.doc);
1513
- this.doc.transact((draft) => {
1514
- const library = draft.part_library ?? {};
1515
- for (const partId of Object.keys(library)) {
1516
- const caption = library[partId]?.caption;
1517
- if (caption == null) continue;
1518
- setPart(draft, partId, { caption: {
1519
- ...caption,
1520
- kind: "caption",
1521
- style: {
1522
- ...caption.style,
1523
- ...input
1524
- }
1525
- } });
1526
- }
1527
- }, audit("SetCaptionStyle", input, options));
1528
- }
1529
- /**
1530
- * Resolve the main-track sequential layout from `source`. Callers inside a
1531
- * `transact` MUST pass the live `draft` so item order reflects in-progress
1532
- * mutations; the default `this.doc.snapshot()` is only committed state and is
1533
- * correct for read-only callers outside a transaction. `readMainTrackItems` /
1534
- * `readPartDurationMs` accept both an immer draft and a raw snapshot.
1535
- */
1536
- computeMainTrackLayout(source = this.doc.snapshot()) {
1537
- const items = readMainTrackItems(source);
1538
- const layout = [];
1539
- let cur = 0;
1540
- for (let i = 0; i < items.length; i++) {
1541
- const partId = items[i].part_id;
1542
- if (partId == null) continue;
1543
- const durationMs = readPartDurationMs(source, partId, 1e3);
1544
- layout.push({
1545
- index: i,
1546
- part_id: partId,
1547
- start_ms: cur,
1548
- end_ms: cur + durationMs
1549
- });
1550
- cur += durationMs;
1551
- }
1552
- return layout;
1553
- }
1554
- indexForStartMs(layout, newStartMs, selfIndex) {
1555
- const without = layout.filter((it) => it.index !== selfIndex);
1556
- let cur = 0;
1557
- let target = without.length;
1558
- for (let i = 0; i < without.length; i++) {
1559
- const it = without[i];
1560
- const durationMs = it.end_ms - it.start_ms;
1561
- if (newStartMs <= cur + durationMs / 2) {
1562
- target = i;
1563
- break;
1564
- }
1565
- cur += durationMs;
1566
- }
1567
- return Math.max(0, Math.min(target, layout.length - 1));
1568
- }
1569
- computeAddInsertIndex(input) {
1570
- const items = readMainTrackItems(this.doc.snapshot());
1571
- if (input.before_clip_id != null) {
1572
- const idx = items.findIndex((it) => it.part_id === input.before_clip_id);
1573
- return idx < 0 ? items.length : idx;
1574
- }
1575
- if (input.after_clip_id != null) {
1576
- const idx = items.findIndex((it) => it.part_id === input.after_clip_id);
1577
- return idx < 0 ? items.length : idx + 1;
1578
- }
1579
- const firstStart = input.clips[0]?.start_ms;
1580
- if (firstStart == null) return items.length;
1581
- const snapshot = this.doc.snapshot();
1582
- let cur = 0;
1583
- for (let i = 0; i < items.length; i++) {
1584
- const partId = items[i].part_id;
1585
- const durationMs = partId == null ? 1e3 : readPartDurationMs(snapshot, partId, 1e3);
1586
- if (firstStart <= cur + durationMs / 2) return i;
1587
- cur += durationMs;
1588
- }
1589
- return items.length;
1590
- }
1591
- };
1592
- function audit(kind, payload, options) {
1593
- return {
1594
- kind,
1595
- payload,
1596
- intent: options?.intent ?? null,
1597
- ...options?.actor ? { actor: options.actor } : {}
1598
- };
1599
- }
1600
- function mainTrackRow(draft) {
1601
- return (draft.tracks ?? []).find((t) => t?.parts_kind === "video_clip");
1602
- }
1603
- /**
1604
- * Locate the main (video_clip) track in the single `tracks` list, minting an
1605
- * empty one in lane-stacking order if absent (reference/17 §4: lane =
1606
- * `parts_kind`). Used by ops that add the first clips into an empty document.
1607
- * Delegates to `ensureLaneTrack` so the caption → main → speech/bgm ordering is
1608
- * enforced in one place.
1609
- */
1610
- function ensureMainTrack(draft) {
1611
- return ensureLaneTrack(draft, "video_clip");
1612
- }
1613
- function mainTrackItems(draft) {
1614
- const track = mainTrackRow(draft);
1615
- if (track == null) return void 0;
1616
- track.items ??= [];
1617
- return track.items;
1618
- }
1619
- /**
1620
- * Resolve a `MoveAnchor` into an insertion slot in the **pre-extraction** item
1621
- * list; the caller adjusts for items removed ahead of it.
1622
- *
1623
- * A missing anchor cannot reach here — `validateMoveVideoClipsByAnchor` rejects it
1624
- * loudly first. This deliberately differs from `computeAddInsertIndex`, which
1625
- * falls back to appending: adding clips with a stale anchor still has an obvious
1626
- * intent (put them somewhere), while moving to a slot that no longer exists does
1627
- * not. The `-1` guard stays as a defence-in-depth for a caller that bypassed
1628
- * validation, and appending is the least destructive reading.
1629
- */
1630
- function anchorInsertIndex(items, anchor) {
1631
- if (anchor.position === "track_start") return 0;
1632
- const index = items.findIndex((item) => item?.part_id === anchor.clip_id);
1633
- if (index < 0) return items.length;
1634
- return anchor.position === "before" ? index : index + 1;
1635
- }
1636
- function moveItem(items, from, to) {
1637
- if (from === to) return;
1638
- const [moved] = items.splice(from, 1);
1639
- items.splice(to, 0, moved);
1640
- }
1641
- function setPart(draft, partId, part) {
1642
- draft.part_library ??= {};
1643
- draft.part_library[partId] = partUnionToDraft(part);
1644
- }
1645
- function deletePart(draft, partId) {
1646
- if (draft.part_library != null) delete draft.part_library[partId];
1647
- }
1648
- /**
1649
- * The validated speed-shift input already matches the domain `SpeedShift` shape
1650
- * (`category` enum + `config` linear/curve union, mirroring the IDL); this just
1651
- * narrows `null` (clear speed back to 1×) to `undefined`.
1652
- */
1653
- function normalizeSpeedShift(input) {
1654
- return input ?? void 0;
1655
- }
1656
- /**
1657
- * Write a materialized speech subtree into the draft: upsert each speech +
1658
- * caption part, and insert/update their lane items with `anchored` `time_position`
1659
- * facts taken verbatim from the input (RFC 02 §4) — speech anchors to its host
1660
- * video clip (`anchor_part_id` + `offset_ms`); caption anchors to its speech
1661
- * (offset = caption part `start_ms`). No absolute time is written; the
1662
- * projection derives it on read. On re-TTS the speech id is preserved, so an
1663
- * existing item is re-positioned rather than duplicated.
1664
- */
1665
- function writeSpeechAssets(draft, assets) {
1666
- const captionById = new Map(assets.captions.map((c) => [c.caption_id, c]));
1667
- const speechTrack = ensureLaneTrack(draft, "speech");
1668
- speechTrack.items ??= [];
1669
- let captionItems;
1670
- const ensureCaptionItems = () => {
1671
- if (captionItems == null) {
1672
- const track = ensureLaneTrack(draft, "caption");
1673
- track.items ??= [];
1674
- captionItems = track.items;
1675
- }
1676
- return captionItems;
1677
- };
1678
- const ranges = mainTrackRanges(draft);
1679
- const hostStartMs = (anchorPartId) => ranges.find((r) => r.partId === anchorPartId)?.startMs;
1680
- for (const speech of assets.speeches) {
1681
- const priorCaptionIds = draft.part_library?.[speech.speech_id]?.speech?.caption_ids ?? [];
1682
- const nextCaptionIds = new Set(speech.caption_ids);
1683
- const removedCaptionIds = priorCaptionIds.filter((id) => id != null && !nextCaptionIds.has(id));
1684
- for (const captionId of removedCaptionIds) deletePart(draft, captionId);
1685
- if (removedCaptionIds.length > 0) {
1686
- const lane = findLaneTrack(draft, "caption");
1687
- const removed = new Set(removedCaptionIds);
1688
- if (lane?.items != null) lane.items = lane.items.filter((it) => it?.part_id == null || !removed.has(it.part_id));
1689
- }
1690
- const priorSpeech = draft.part_library?.[speech.speech_id]?.speech;
1691
- setPart(draft, speech.speech_id, { speech: {
1692
- ...priorSpeech,
1693
- id: speech.speech_id,
1694
- kind: "speech",
1695
- media_duration_ms: speech.duration_ms,
1696
- audio_script: speech.audio_script,
1697
- volume: speech.volume,
1698
- audio_storage_key: speech.audio_storage_key,
1699
- origin_speech_id: speech.origin_speech_id,
1700
- voice: speech.voice,
1701
- caption_ids: speech.caption_ids
1702
- } });
1703
- const hostStart = hostStartMs(speech.anchor_part_id);
1704
- const speechAbs = hostStart == null ? void 0 : hostStart + speech.offset_ms;
1705
- placeRelative(speechTrack.items, speech.speech_id, speech.anchor_part_id, speech.offset_ms, speechAbs);
1706
- for (const captionId of speech.caption_ids) {
1707
- const caption = captionById.get(captionId);
1708
- if (caption == null) continue;
1709
- const priorCaption = draft.part_library?.[captionId]?.caption;
1710
- setPart(draft, captionId, { caption: {
1711
- ...priorCaption ?? { style: {} },
1712
- id: captionId,
1713
- kind: "caption",
1714
- initial_duration_ms: caption.duration_ms,
1715
- speech_part_id: speech.speech_id,
1716
- text: caption.text,
1717
- start_ms: caption.start_ms
1718
- } });
1719
- const captionAbs = speechAbs == null ? void 0 : speechAbs + caption.start_ms;
1720
- placeRelative(ensureCaptionItems(), captionId, speech.speech_id, caption.start_ms, captionAbs);
1721
- }
1722
- }
1723
- }
1724
- /**
1725
- * Insert a lane item with an `anchored` time position fact, or re-position it if
1726
- * already present. `fallbackAbsMs` is the orphan-recovery snapshot (RFC 02
1727
- * §11.1): the item's absolute landing the caller resolved from its anchor, so a
1728
- * freshly-created anchored item can still be recovered if its anchor is later
1729
- * concurrently deleted. Pass `undefined` only when the anchor's absolute
1730
- * position cannot be resolved (no host clip yet).
1731
- */
1732
- function placeRelative(items, partId, anchorPartId, offsetMs, fallbackAbsMs) {
1733
- const timePosition = {
1734
- mode: "anchored",
1735
- anchorPartId,
1736
- offsetMs: Math.max(0, Math.round(offsetMs))
1737
- };
1738
- const existing = items.find((it) => it?.part_id === partId);
1739
- if (existing != null) {
1740
- existing.time_position = timePosition;
1741
- if (fallbackAbsMs != null) existing.fallback_abs_ms = fallbackAbsMs;
1742
- return;
1743
- }
1744
- items.push({
1745
- part_id: partId,
1746
- time_position: timePosition,
1747
- fallback_abs_ms: fallbackAbsMs
1748
- });
1749
- }
1750
- /**
1751
- * After the main-track flow order changes (move / reorder), re-parent each
1752
- * anchored speech to the clip its *preserved* absolute landing now falls in
1753
- * (RFC 02 §9.1). `beforeRanges` is the pre-change main-track layout, used only
1754
- * to recover each speech's current absolute time; the new layout is read fresh.
1755
- * Writes only the affected speech `position` facts — a single forward pass, not
1756
- * a cascade. Captions follow their speech (relative to it), so they need no
1757
- * rewrite.
1758
- */
1759
- function reparentSpeechesAfterMainTrackChange(draft, beforeRanges) {
1760
- const speechTrack = findLaneTrack(draft, "speech");
1761
- if (speechTrack?.items == null) return;
1762
- const beforeStart = new Map(beforeRanges.map((r) => [r.partId, r.startMs]));
1763
- const afterRanges = mainTrackRanges(draft);
1764
- for (const item of speechTrack.items) {
1765
- const timePosition = item?.time_position;
1766
- if (timePosition?.mode !== "anchored") continue;
1767
- const hostStart = beforeStart.get(timePosition.anchorPartId);
1768
- if (hostStart == null) continue;
1769
- const currentAbs = hostStart + timePosition.offsetMs;
1770
- item.time_position = relativePositionForAbs(afterRanges, currentAbs);
1771
- item.fallback_abs_ms = currentAbs;
1772
- }
1773
- }
1774
- /**
1775
- * Refresh the orphan-recovery snapshot (`fallback_abs_ms`, RFC 02 §11.1) of
1776
- * every `anchored` speech from the current main-track layout, keeping the
1777
- * `time_position` facts untouched. Called after an op that shifts clip positions
1778
- * without re-parenting (e.g. re-trim), so a later orphan recovery uses an
1779
- * absolute landing that matches the post-edit layout, not a stale one. Speeches
1780
- * whose anchor clip is absent are left as-is (recovery handles them).
1781
- */
1782
- function refreshAnchoredFallbackAbs(draft) {
1783
- const speechTrack = findLaneTrack(draft, "speech");
1784
- if (speechTrack?.items == null) return;
1785
- const startByPart = new Map(mainTrackRanges(draft).map((r) => [r.partId, r.startMs]));
1786
- for (const item of speechTrack.items) {
1787
- const timePosition = item?.time_position;
1788
- if (timePosition?.mode !== "anchored") continue;
1789
- const hostStart = startByPart.get(timePosition.anchorPartId);
1790
- if (hostStart == null) continue;
1791
- item.fallback_abs_ms = hostStart + timePosition.offsetMs;
1792
- }
1793
- }
1794
- /**
1795
- * Remove a speech part, its captions, and all their lane items (RFC 02 §9.2
1796
- * subtree). The cascade basis is the anchored `time_position`, not the speech's
1797
- * `caption_ids` (reference/17 §6): a caption is deleted iff it is `anchored` to
1798
- * this speech (`time_position.anchorPartId === speechId`). `caption_ids` stays as
1799
- * a resource-intrinsic fact (reference/17 principle 4) but no longer drives
1800
- * deletion — "what to delete" is decoupled from "what the resource is".
1801
- */
1802
- function deleteSpeechSubtree(draft, speechId) {
1803
- const captionTrack = findLaneTrack(draft, "caption");
1804
- 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));
1805
- deletePart(draft, speechId);
1806
- for (const captionId of anchoredCaptionIds) deletePart(draft, captionId);
1807
- const speechTrack = findLaneTrack(draft, "speech");
1808
- if (speechTrack?.items != null) speechTrack.items = speechTrack.items.filter((it) => it?.part_id !== speechId);
1809
- if (captionTrack?.items != null) captionTrack.items = captionTrack.items.filter((it) => it?.part_id == null || !anchoredCaptionIds.has(it.part_id));
1810
- }
1811
- /**
1812
- * Detach the direct anchored children of a video clip (reference/17 §6 `detach`):
1813
- * each speech anchored to `clipId` is re-pinned to `{ mode:'absolute' }` at its
1814
- * current projected landing, so it stays on the timeline once the clip is gone.
1815
- * Computed while the anchor chain is intact (call before removing the clip), so
1816
- * the absolute landing is always resolvable — no `fallback_abs_ms` read needed.
1817
- *
1818
- * Only the *direct* children detach: a speech's captions anchor to the speech
1819
- * (not the clip), so they keep following the speech and need no rewrite.
1820
- */
1821
- function detachAnchoredChildren(draft, clipId) {
1822
- const speechTrack = findLaneTrack(draft, "speech");
1823
- if (speechTrack?.items == null) return;
1824
- const clipStart = new Map(mainTrackRanges(draft).map((r) => [r.partId, r.startMs])).get(clipId);
1825
- for (const item of speechTrack.items) {
1826
- const timePosition = item?.time_position;
1827
- if (timePosition?.mode !== "anchored" || timePosition.anchorPartId !== clipId) continue;
1828
- const abs = (clipStart ?? item.fallback_abs_ms ?? 0) + timePosition.offsetMs;
1829
- item.time_position = {
1830
- mode: "absolute",
1831
- offsetMs: Math.max(0, Math.round(abs))
1832
- };
1833
- item.fallback_abs_ms = void 0;
1834
- }
1835
- }
1836
- /**
1837
- * Re-anchor the direct anchored children of `fromClipId` onto `toClipId`, keeping
1838
- * each child's `offsetMs` unchanged.
1839
- *
1840
- * "Same offset into the replacement" is the whole point: a speech that started
1841
- * 100ms into the clip it narrated still starts 100ms into the clip that replaced
1842
- * it, whatever the two clips' durations are. Absolute time is NOT preserved here
1843
- * (that is `moveVideoClips`' rule, where the clip stays and the timeline reflows
1844
- * around it) — the clip itself is being swapped out, so following it is what keeps
1845
- * narration attached to the picture it describes.
1846
- *
1847
- * `fallback_abs_ms` is deliberately left alone: it is the orphan-recovery snapshot
1848
- * (RFC 02 §11.1), refreshed by the projection on the next read. Writing a guess
1849
- * here would make this op compete with the follow-the-clock refresh for the same
1850
- * LWW unit.
1851
- *
1852
- * Only direct children move: captions anchor to their speech, so they follow it
1853
- * without a rewrite.
1854
- */
1855
- function reanchorAnchoredChildren(draft, fromClipId, toClipId) {
1856
- const speechTrack = findLaneTrack(draft, "speech");
1857
- if (speechTrack?.items == null) return;
1858
- for (const item of speechTrack.items) {
1859
- const timePosition = item?.time_position;
1860
- if (timePosition?.mode !== "anchored" || timePosition.anchorPartId !== fromClipId) continue;
1861
- item.time_position = {
1862
- ...timePosition,
1863
- anchorPartId: toClipId
1864
- };
1865
- }
1866
- }
1867
- /** Delete every speech (with its captions) anchored to the given video clip (RFC 02 §9.2). */
1868
- function deleteAnchoredSubtree(draft, clipId) {
1869
- 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);
1870
- for (const speechId of anchored) deleteSpeechSubtree(draft, speechId);
1871
- }
1872
- //#endregion
1873
- //#region src/editor/types.ts
1874
- /** Runtime list of the frozen, implemented kinds (for guards / introspection). */
1875
- const IMPLEMENTED_SEMANTIC_OP_KINDS = [
1876
- "MoveVideoClips",
1877
- "MoveVideoClipsByAnchor",
1878
- "DeleteVideoClips",
1879
- "AddVideoClips",
1880
- "AdjustVideoClipVolume",
1881
- "SetVideoClipSpeedShift",
1882
- "ReplaceVideoClipContent",
1883
- "ReplaceVideoClipSequence",
1884
- "AdjustVideoClipDuration",
1885
- "AddSpeeches",
1886
- "DeleteSpeeches",
1887
- "MoveSpeeches",
1888
- "ChangeSpeechScript",
1889
- "ChangeSpeechVoice",
1890
- "AdjustSpeechVolume",
1891
- "SetCaptionVisibility",
1892
- "SetCaptionStyle",
1893
- "SetBgm",
1894
- "DeleteBgm",
1895
- "AdjustBgmVolume"
1896
- ];
1897
- function isImplementedSemanticOpKind(kind) {
1898
- return IMPLEMENTED_SEMANTIC_OP_KINDS.includes(kind);
1899
- }
1900
- //#endregion
1901
- //#region src/manual-sync/doc-version-mark.ts
1902
- /**
1903
- * Encode a {@link DocVersionMark} for storage or transport.
1904
- *
1905
- * The mark stays opaque across the round trip — the string is not a version
1906
- * number and must not be compared, ordered, or parsed. Its only use is
1907
- * {@link decodeDocVersionMark} followed by `hasChangedSince`.
1908
- *
1909
- * Callers that persist this should know the encoded length grows with the number
1910
- * of peers that have ever written to the document (one counter each), and the FE
1911
- * mints a fresh peer per page load. Still small in practice (a few hundred bytes
1912
- * for dozens of peers), but it grows with document age rather than size; version
1913
- * vector compaction is deferred to a later phase.
1914
- */
1915
- function encodeDocVersionMark(mark) {
1916
- return bytesToBase64(mark.encoded);
1917
- }
1918
- /**
1919
- * Rebuild a mark from {@link encodeDocVersionMark}'s output.
1920
- *
1921
- * Returns `undefined` for input this did not produce (a legacy integer version,
1922
- * a truncated value, an empty string). That is the honest answer — "I cannot
1923
- * establish what you last saw" — and callers should treat it as "no baseline"
1924
- * rather than as "unchanged". Decoding does not validate the bytes as a version
1925
- * vector; `hasChangedSince` reports "changed" for an undecodable mark, which is
1926
- * the conservative direction.
1927
- */
1928
- function decodeDocVersionMark(encoded) {
1929
- if (encoded === "") return void 0;
1930
- try {
1931
- return {
1932
- __brand: "mengine-doc-version-mark",
1933
- encoded: base64ToBytes(encoded)
1934
- };
1935
- } catch {
1936
- return;
1937
- }
1938
- }
1939
- //#endregion
1940
- //#region src/manual-sync/version-coverage.ts
1941
- /**
1942
- * Version-vector coverage: "does `outer` contain everything in `inner`?"
1943
- *
1944
- * This one predicate answers three different questions in the manual-sync document, which
1945
- * is why it is factored out rather than inlined three times:
1946
- *
1947
- * | question | call |
1948
- * | ------------------------------------- | ------------------------------- |
1949
- * | is there anything left to push? | `covers(watermark, localOplog)` |
1950
- * | did someone else write concurrently? | `covers(localOplog, serverVV)` |
1951
- * | has the doc moved since I last read? | `covers(seenVersion, localOplog)`|
1952
- *
1953
- * `VersionVector.compare` cannot be used for any of them: it returns `undefined`
1954
- * for concurrent vectors, and concurrency is the NORMAL case here — the server
1955
- * routinely holds peers the local doc has never seen, and after a collaborative
1956
- * merge the local doc holds ops the watermark predates. Treating "concurrent" as
1957
- * "not covered" is right for some of these and wrong for others, so the per-peer
1958
- * counter check is the only formulation that stays correct for all three.
1959
- *
1960
- * Equality must NOT be used as a substitute either: once collaboration happens
1961
- * the watermark legitimately *leads* the local doc (it carries other peers'
1962
- * counters), so an equality test reports "still has ops to push" forever.
1963
- */
1964
- function covers(outer, inner) {
1965
- for (const [peer, counter] of inner.toJSON()) if ((outer.get(peer) ?? 0) < counter) return false;
1966
- return true;
1967
- }
1968
- //#endregion
7
+ import { DisposableSet, EventBus } from "@mengine/utils";
1969
8
  //#region src/manual-sync/manual-sync-doc.ts
1970
- /**
1971
- * Agent-facing document with explicit `pull()` / `push()` over one Loro
1972
- * document, with no background sync (ADR 0015 D1–D4).
1973
- *
1974
- * It deliberately does NOT reuse `MengineDocSession`'s stack. That stack —
1975
- * SSE + local `DocStorage` + `ClientServerSynchronizer` + `DocManager` — is
1976
- * correct for a browser editor and actively wrong here:
1977
- *
1978
- * - Its retry backoff lands *outside* the tool-call lifetime, so a write can
1979
- * settle seconds after the tool already told the LLM what happened.
1980
- * - It has no durable local queue in the harness, so pending pushes die with the
1981
- * process — that is lost data, reported as success.
1982
- * - Agent semantics require the document to change only at points the agent can
1983
- * name. If it converged on its own between tool calls, "what state was this
1984
- * decision based on" would be unanswerable, and a remote change could land
1985
- * mid-edit.
1986
- *
1987
- * What replaces the whole background job queue is one variable: the **watermark**,
1988
- * the version the server has confirmed. `push()` exports `{mode:'update', from:
1989
- * watermark}` and only advances it on a confirmed verdict, so a failed push is
1990
- * retried implicitly — the next push carries both the failed ops and any new
1991
- * ones, in one blob. No queue, no timer, no retry bookkeeping.
1992
- *
1993
- * Two non-obvious properties of that watermark, both verified against a real
1994
- * server in the ADR 0015 spike:
1995
- *
1996
- * - It can legitimately *lead* the local document (it carries other peers'
1997
- * counters). Exporting `from` a leading watermark does not error; the blob
1998
- * correctly contains only the local peer's new ops. So a collaborative merge
1999
- * does not force a pull before pushing.
2000
- * - Therefore every emptiness/coverage test must be one-directional containment,
2001
- * never equality. See {@link covers}.
2002
- *
2003
- * Lifecycle: one `LoroDoc` per document, shared across agent loops with a
2004
- * refcount held by the caller's session registry. Rebuilding the doc per tool
2005
- * call would mint a new peer each time and permanently inflate the document's
2006
- * version vector for every future reader.
2007
- *
2008
- * Not thread-safe by design and it does not need to be: harness tool calls run
2009
- * serially (`execToolCalls` is a `for` + `await`).
2010
- */
2011
- var ManualSyncDoc = class ManualSyncDoc {
2012
- client;
2013
- doc;
2014
- adapter;
9
+ /** Explicit synchronization over the same DSL/editor used by the live Session. */
10
+ var ManualSyncDoc = class ManualSyncDoc extends ManualSyncTransport {
11
+ dsl;
2015
12
  editor;
2016
- /**
2017
- * The version the server is known to hold. Starts empty (nothing confirmed)
2018
- * and only ever moves forward on a verdict that proves the server took our ops.
2019
- */
2020
- watermark = new VersionVector(/* @__PURE__ */ new Map());
2021
- constructor(client, doc) {
2022
- this.client = client;
2023
- this.doc = doc;
2024
- this.adapter = new MirrorVideoDocumentAdapter(doc);
2025
- this.editor = new SemanticEditor(this.adapter);
13
+ constructor(client, doc, watermark) {
14
+ super(client, doc, watermark);
15
+ assertDslDocument(doc);
16
+ this.dsl = new MedeoDsl(doc);
17
+ this.editor = new DslEditor(this.dsl);
2026
18
  }
2027
- /**
2028
- * Open an existing server document.
2029
- *
2030
- * Fetches the snapshot up front rather than starting empty and converging: the
2031
- * agent's first act is to read the document, so there is no useful state before
2032
- * the snapshot lands. This also fails fast and loudly on a document that does
2033
- * not exist or whose first snapshot is incomplete or invalid.
2034
- */
2035
- static async open(options) {
2036
- const response = await options.client.fetchSnapshot();
2037
- const doc = new LoroDoc();
2038
- let manualSyncDoc;
2039
- try {
2040
- if (options.peerId != null) doc.setPeerId(options.peerId);
2041
- if (doc.import(base64ToBytes(response.snapshot)).pending != null) throw new Error("Initial mengine snapshot has missing dependencies");
2042
- manualSyncDoc = new ManualSyncDoc(options.client, doc);
2043
- assertValidVideoDocument(manualSyncDoc.snapshot());
2044
- manualSyncDoc.watermark = manualSyncDoc.serverVVFrom(response.version.server_vv) ?? doc.oplogVersion();
2045
- return manualSyncDoc;
2046
- } catch (error) {
2047
- manualSyncDoc?.adapter.dispose();
2048
- doc.free();
2049
- throw error;
2050
- }
19
+ static open(options) {
20
+ return openManualDocument(options, (doc, watermark) => new ManualSyncDoc(options.client, doc, watermark));
2051
21
  }
2052
- /** Current document read model (authoritative shape). */
2053
22
  snapshot() {
2054
- return this.adapter.snapshot();
2055
- }
2056
- /**
2057
- * The Loro peer this document writes as.
2058
- *
2059
- * Exposed because the peer is an externally-meaningful fact, not an internal
2060
- * detail: it is the identity every op this document emits is attributed to, and
2061
- * callers mint it under rules of their own (the harness reserves a range so a
2062
- * peer id alone says "Agent wrote this"). Being able to read it back means those
2063
- * rules can be verified against the live document rather than against whatever
2064
- * was passed to the constructor.
2065
- */
2066
- editorPeerId() {
2067
- return this.doc.peerIdStr;
2068
- }
2069
- /** Current content projected into the compatible track and part read shape. */
2070
- content() {
2071
- return fromVideoDocument(this.adapter.snapshot());
2072
- }
2073
- /**
2074
- * Mark the document state the caller has just observed, for a later
2075
- * {@link hasChangedSince}.
2076
- *
2077
- * This pair replaces the legacy integer-version comparison that
2078
- * `DraftVersionDetector` used to tell the LLM "the draft was modified
2079
- * externally, reload before editing". Comparing Loro version vectors covers
2080
- * all collaborative edits without a business revision field in the document.
2081
- *
2082
- * Same capability, not a stronger one: like the legacy detector, this only
2083
- * reports what changed between two moments the caller chose to sample.
2084
- */
2085
- versionMark() {
2086
- return {
2087
- __brand: "mengine-doc-version-mark",
2088
- encoded: this.doc.oplogVersion().encode()
2089
- };
2090
- }
2091
- /**
2092
- * Has the document moved since `mark` was taken?
2093
- *
2094
- * Reports any advance, whoever caused it — including this document's own edits.
2095
- * The caller decides what is interesting: a detector sampling once per turn is
2096
- * asking "did anything happen", and its own edits legitimately count.
2097
- */
2098
- hasChangedSince(mark) {
2099
- let previous;
2100
- try {
2101
- previous = VersionVector.decode(mark.encoded);
2102
- } catch {
2103
- return true;
2104
- }
2105
- return !covers(previous, this.doc.oplogVersion());
2106
- }
2107
- /**
2108
- * Fetch and merge everything the server has that this document lacks.
2109
- *
2110
- * Must run *before* the editor on each tool call. `SemanticEditor` validates
2111
- * against the local document, so editing a stale one validates against a world
2112
- * that no longer exists: the ADR 0015 spike confirmed that without pull-first
2113
- * an edit to a clip another writer had already deleted passes validation and is
2114
- * accepted by the server. Pulling afterwards cannot undo that.
2115
- *
2116
- * A failure is returned, not thrown — a transient network blip must not make
2117
- * the tool unusable (M2/A ruling; legacy tolerates read failures the same way).
2118
- * The caller proceeds on a possibly-stale document knowingly.
2119
- */
2120
- async pull() {
2121
- const before = this.doc.oplogVersion();
2122
- try {
2123
- const update = base64ToBytes((await this.client.sync(before.encode())).update);
2124
- if (update.byteLength > 0) this.doc.import(update);
2125
- return {
2126
- ok: true,
2127
- changed: !covers(before, this.doc.oplogVersion())
2128
- };
2129
- } catch (error) {
2130
- return {
2131
- ok: false,
2132
- reason: "failed",
2133
- error: error instanceof Error ? error : new Error(String(error))
2134
- };
2135
- }
23
+ this.assertActive();
24
+ return this.dsl.getSnapshot();
2136
25
  }
2137
- /**
2138
- * Push every local op the server has not confirmed, and report the verdict.
2139
- *
2140
- * The return value is the durability answer a tool needs before claiming
2141
- * success: only `ack` / `duplicate` mean the server holds the ops. This is why
2142
- * this class exists rather than an ack-waiter — with a direct call, "did it
2143
- * land" is simply the result.
2144
- *
2145
- * `duplicate` counts as durable: the bytes added nothing *because* the server
2146
- * already had them.
2147
- */
2148
- async push() {
2149
- const localVersion = this.doc.oplogVersion();
2150
- if (covers(this.watermark, localVersion)) return {
2151
- kind: "nothing_to_push",
2152
- collaborated: false
2153
- };
2154
- const update = this.doc.export({
2155
- mode: "update",
2156
- from: this.watermark
2157
- });
2158
- try {
2159
- const response = await this.client.pushUpdate(update);
2160
- const serverVV = this.serverVVFrom(response.version?.server_vv);
2161
- if (serverVV != null) this.watermark = serverVV;
2162
- return {
2163
- kind: response.kind,
2164
- mengineUpdateSeq: response.version?.update_seq ?? response.update_seq ?? void 0,
2165
- updateSeq: response.update_seq ?? void 0,
2166
- collaborated: serverVV != null && !covers(localVersion, serverVV)
2167
- };
2168
- } catch (error) {
2169
- const failure = error instanceof Error ? error : new Error(String(error));
2170
- const rejected = error instanceof MenginePushRejectedError;
2171
- return {
2172
- kind: rejected ? "rejected" : "failed",
2173
- collaborated: false,
2174
- code: rejected ? error.code : void 0,
2175
- error: failure
2176
- };
2177
- }
26
+ projectVideoDraft() {
27
+ return toVideoDraft(this.snapshot());
2178
28
  }
2179
- /** Decode a wire `server_vv`, tolerating absence/corruption (never throws). */
2180
- serverVVFrom(serverVV) {
2181
- if (serverVV == null || serverVV === "") return void 0;
2182
- try {
2183
- return VersionVector.decode(base64ToBytes(serverVV));
2184
- } catch {
2185
- return;
2186
- }
29
+ dispose() {
30
+ this.dsl.dispose();
31
+ super.dispose();
2187
32
  }
2188
33
  };
2189
34
  //#endregion
2190
- //#region src/storage/medeo-http-doc-storage.ts
2191
- /**
2192
- * SSE-backed {@link Connection} for the mengine-server document stream.
2193
- *
2194
- * It owns the live SSE read loop and reflects its lifecycle as connection
2195
- * status: `connecting` while (re)establishing, `connected` once the stream is
2196
- * open, back to `connecting` on a drop (auto-reconnect), `closed` on
2197
- * `disconnect`. Reporting a drop as a status change is the whole point — the
2198
- * synchronizer watches `onStatusChanged` and, on any change, tears down and
2199
- * re-runs its connect cycle, which re-issues `getDocDiff(doc.version())` and so
2200
- * recovers whatever the stream missed while it was down. Recovery therefore
2201
- * lives in the synchronizer (keyed on the real doc version), not here.
2202
- *
2203
- * Update frames are forwarded via `onUpdate`, resync hints via `onResync`; this
2204
- * connection keeps no version cursor and does no catch-up of its own. Dedup is
2205
- * unnecessary because `LoroDoc.import` is idempotent by OpId/VV.
2206
- */
2207
- var SseConnection = class {
2208
- client;
2209
- onUpdate;
2210
- onResync;
2211
- reconnectDelayMs;
2212
- inner = void 0;
2213
- event = new EventBus();
2214
- streamTask = null;
2215
- _status = "idle";
2216
- _error;
2217
- constructor(client, onUpdate, onResync, reconnectDelayMs) {
2218
- this.client = client;
2219
- this.onUpdate = onUpdate;
2220
- this.onResync = onResync;
2221
- this.reconnectDelayMs = reconnectDelayMs;
2222
- }
2223
- get status() {
2224
- return this._status;
2225
- }
2226
- get error() {
2227
- return this._error;
2228
- }
2229
- connect() {
2230
- if (this.streamTask != null) return;
2231
- const task = Task.spawn((scope) => this.runStreamLoop(scope.signal));
2232
- this.streamTask = task;
2233
- const release = () => {
2234
- if (this.streamTask === task) this.streamTask = null;
2235
- };
2236
- task.then(release, release);
2237
- }
2238
- disconnect() {
2239
- this.streamTask?.cancel();
2240
- this.streamTask = null;
2241
- this.setStatus("closed");
2242
- }
2243
- waitForConnected() {
2244
- return new Task((resolve, reject, scope) => {
2245
- if (this._status === "connected") {
2246
- resolve();
2247
- return;
2248
- }
2249
- const off = this.onStatusChanged((status, error) => {
2250
- if (status === "connected") {
2251
- off();
2252
- resolve();
2253
- } else if (status === "closed") {
2254
- off();
2255
- reject(error ?? /* @__PURE__ */ new Error("SSE connection closed"));
2256
- }
2257
- });
2258
- scope.disposer.add(off);
2259
- });
2260
- }
2261
- onStatusChanged(cb) {
2262
- return this.event.on("statusChanged", ({ status, error }) => cb(status, error));
2263
- }
2264
- setStatus(status, error) {
2265
- if (this._status === status && this._error === error) return;
2266
- this._status = status;
2267
- this._error = error;
2268
- this.event.emit("statusChanged", {
2269
- status,
2270
- error
2271
- });
2272
- }
2273
- async runStreamLoop(signal) {
2274
- while (!signal.aborted) {
2275
- this.setStatus("connecting");
2276
- try {
2277
- await readMengineEventStream({
2278
- client: this.client,
2279
- signal,
2280
- onOpen: () => this.setStatus("connected"),
2281
- onResync: this.onResync,
2282
- onUpdate: (event) => {
2283
- for (const update of event.updates) this.onUpdate(update);
2284
- }
2285
- });
2286
- if (!signal.aborted) this.setStatus("connecting", /* @__PURE__ */ new Error("SSE stream closed"));
2287
- } catch (error) {
2288
- if (signal.aborted) break;
2289
- this.setStatus("connecting", error instanceof Error ? error : new Error(String(error)));
2290
- }
2291
- if (signal.aborted) break;
2292
- try {
2293
- await Task.delay(this.reconnectDelayMs).abortOn(signal);
2294
- } catch {
2295
- break;
2296
- }
2297
- }
2298
- }
2299
- };
35
+ //#region src/session/document-mutation-guard.ts
2300
36
  /**
2301
- * Adapts the mengine-server HTTP/SSE protocol to the engine `DocStorage`
2302
- * contract so `ClientServerSynchronizer` can treat it as a remote peer.
2303
- *
2304
- * Deliberately thin (mirrors the socket `DocStorage` in the playground): it
2305
- * forwards live SSE updates and exposes a version-vector diff, and keeps NO
2306
- * sync state of its own.
2307
- *
2308
- * - `getDocDiff(docId, knownVersion)` pulls the server-computed VV-diff via
2309
- * `GET /sync?from=<vv>` — the synchronizer passes the real `doc.version()`, so
2310
- * the response carries exactly the ops the doc is missing. `getDoc` (full
2311
- * `/snapshot`) stays for cold start, when the caller holds no version yet.
2312
- * - `pushDocUpdate` forwards a Loro update; the server appends it.
2313
- * - `subscribeDocUpdate` registers a callback for live SSE updates. It does no
2314
- * catch-up and keeps no cursor: after an SSE drop the connection reports a
2315
- * status change, and the synchronizer re-runs its cycle to catch up via
2316
- * `getDocDiff(doc.version())`. `LoroDoc.import` is idempotent (OpId/VV), so
2317
- * re-forwarded or echoed updates are harmless.
2318
- *
2319
- * It is bound to a single `docId` because `MengineHttpClient` is per-document.
37
+ * @internal
38
+ * Shared by synchronous mutation entry points for one document. The session
39
+ * closes it before teardown callbacks can attempt another write.
2320
40
  */
2321
- var MedeoHttpDocStorage = class {
2322
- options;
2323
- connection;
2324
- client;
2325
- docId;
2326
- events = new EventBus();
2327
- constructor(options) {
2328
- this.options = options;
2329
- this.client = options.client;
2330
- this.docId = options.docId;
2331
- this.connection = new SseConnection(this.client, (update) => this.emitUpdate(update), () => this.events.emit("resync"), options.sseReconnectDelayMs ?? 500);
2332
- }
2333
- get isReadonly() {
2334
- return this.options.readonlyMode ?? false;
2335
- }
2336
- async getDoc(docId) {
2337
- this.assertDocId(docId);
2338
- const snapshot = await this.client.fetchSnapshot();
2339
- this.reportServerVersion(snapshot.version?.server_vv);
2340
- const now = /* @__PURE__ */ new Date();
2341
- return {
2342
- docId,
2343
- data: base64ToBytes(snapshot.snapshot),
2344
- createdAt: now,
2345
- updatedAt: now
2346
- };
41
+ var DocumentMutationGuard = class {
42
+ readonlyMode;
43
+ running = false;
44
+ closed = false;
45
+ constructor(readonlyMode = false) {
46
+ this.readonlyMode = readonlyMode;
2347
47
  }
2348
- async getDocDiff(docId, knownVersion) {
2349
- this.assertDocId(docId);
2350
- const response = await this.client.sync(knownVersion);
2351
- this.reportServerVersion(response.server_vv);
2352
- return {
2353
- docId,
2354
- missing: base64ToBytes(response.update),
2355
- version: base64ToBytes(response.server_vv)
2356
- };
2357
- }
2358
- /**
2359
- * Forward one update to the server, returning what the server says it now holds.
2360
- *
2361
- * The returned `server_vv` is the server's own statement about itself, computed
2362
- * inside the write transaction. A caller tracking "what the remote has" can
2363
- * adopt it directly, which is strictly better than inferring that bound from
2364
- * the pushed blob: it also covers ops other peers wrote, so those stop being
2365
- * re-sent on every later push. `ack` and `duplicate` both carry it.
2366
- *
2367
- * The verdict itself stays on {@link subscribePushOutcome}, which reports
2368
- * failures too — a return value cannot. Previously the verdict was read and
2369
- * dropped, so a `duplicate` (bytes contributed nothing) was indistinguishable
2370
- * from a successful write.
2371
- *
2372
- * The error is still rethrown after being published: the synchronizer treats a
2373
- * throw as "retry this cycle", and swallowing it here would strand the update.
2374
- * Publishing is therefore additive observability, not error handling.
2375
- */
2376
- async pushDocUpdate(update, _origin) {
2377
- this.assertDocId(update.docId);
2378
- if (this.isReadonly) throw new Error(`MedeoHttpDocStorage is readonly; refusing to push ${update.docId}`);
2379
- if (update.data.byteLength === 0) return {};
48
+ run(mutation) {
49
+ if (this.closed) throw new Error("mengine document is disposed");
50
+ if (this.readonlyMode) throw new Error("mengine document is read-only");
51
+ if (this.running) throw new Error("mengine document mutation is already running");
52
+ this.running = true;
2380
53
  try {
2381
- const response = await this.client.pushUpdate(update.data);
2382
- const serverVV = this.reportServerVersion(response.version?.server_vv);
2383
- this.events.emit("pushOutcome", {
2384
- kind: response.kind,
2385
- updateSeq: response.update_seq ?? void 0,
2386
- serverVV
2387
- });
2388
- return { version: serverVV };
2389
- } catch (error) {
2390
- const failure = error instanceof Error ? error : new Error(String(error));
2391
- const rejected = error instanceof MenginePushRejectedError;
2392
- this.events.emit("pushOutcome", {
2393
- kind: rejected ? "rejected" : "failed",
2394
- code: rejected ? error.code : void 0,
2395
- error: failure
2396
- });
2397
- throw error;
2398
- }
2399
- }
2400
- /**
2401
- * Observe the server's verdict for every pushed update, including failures.
2402
- *
2403
- * This is the loud channel the `void`-returning `DocStorage.pushDocUpdate`
2404
- * cannot express. Consumers that need "did my write land" (the agent's
2405
- * tool-level ack wait) subscribe here.
2406
- */
2407
- subscribePushOutcome(callback) {
2408
- return this.events.on("pushOutcome", callback);
2409
- }
2410
- /** Observe live hints that require VV catch-up without replacing the SSE connection. */
2411
- subscribeResync(callback) {
2412
- return this.events.on("resync", callback);
2413
- }
2414
- /** Authoritative HTTP reads and accepted pushes, never inferred from SSE bytes. */
2415
- subscribeServerVersion(callback) {
2416
- return this.events.on("serverVersion", callback);
2417
- }
2418
- reportServerVersion(encoded) {
2419
- const version = decodeServerVV(encoded);
2420
- if (version != null) this.events.emit("serverVersion", version);
2421
- return version;
2422
- }
2423
- async deleteDoc(_docId) {
2424
- throw new Error("MedeoHttpDocStorage does not support deleteDoc");
2425
- }
2426
- subscribeDocUpdate(callback) {
2427
- return this.events.on("update", ({ update, origin }) => callback(update, origin));
2428
- }
2429
- assertDocId(docId) {
2430
- if (docId !== this.docId) throw new Error(`MedeoHttpDocStorage is bound to ${this.docId}, received ${docId}`);
2431
- }
2432
- emitUpdate(base64Update) {
2433
- this.events.emit("update", {
2434
- update: {
2435
- docId: this.docId,
2436
- data: base64ToBytes(base64Update)
2437
- },
2438
- origin: void 0
2439
- });
2440
- }
2441
- };
2442
- /**
2443
- * Decode the wire `server_vv` into raw bytes, tolerating absence. A server that
2444
- * omits the version block still yields a usable outcome (the kind is the useful
2445
- * part); only the version-coverage wait degrades, so this must not throw.
2446
- */
2447
- function decodeServerVV(serverVV) {
2448
- if (serverVV == null || serverVV === "") return void 0;
2449
- try {
2450
- return base64ToBytes(serverVV);
2451
- } catch {
2452
- return;
2453
- }
2454
- }
2455
- //#endregion
2456
- //#region src/storage/memory-doc-storage.ts
2457
- /**
2458
- * Runtime-neutral local `DocStorage` backed by in-process memory.
2459
- *
2460
- * `IndexedDBDocStorage` is the browser-side local storage, but it requires
2461
- * `indexedDB`/`idb`, which is absent in Node (FE/agent tests, unit tests, SSR).
2462
- * The mengine runtime injects this implementation as the local peer in those
2463
- * environments so the same `DocManager` + `ClientServerSynchronizer` wiring
2464
- * works without a browser. It mirrors the merge-on-read and update-sequence
2465
- * behavior of `IndexedDBDocStorage` so sync semantics are identical.
2466
- */
2467
- var MemoryDocStorage = class extends BaseDocStorage {
2468
- connection = new DummyConnection();
2469
- entries = /* @__PURE__ */ new Map();
2470
- constructor(options = {}) {
2471
- super(options);
2472
- }
2473
- async pushDocUpdate(update, origin) {
2474
- const entry = this.entry(update.docId);
2475
- if (entry.snapshot == null && entry.updates.length === 0) {
2476
- const now = this.now();
2477
- entry.snapshot = {
2478
- docId: update.docId,
2479
- data: update.data,
2480
- createdAt: now,
2481
- updatedAt: now
2482
- };
2483
- } else {
2484
- entry.seq += 1;
2485
- entry.updates.push({
2486
- docId: update.docId,
2487
- seq: entry.seq,
2488
- data: update.data,
2489
- createdAt: this.now()
2490
- });
2491
- }
2492
- this.event.emit("update", {
2493
- update: {
2494
- docId: update.docId,
2495
- data: update.data
2496
- },
2497
- origin
2498
- });
2499
- return {};
2500
- }
2501
- async deleteDoc(docId) {
2502
- this.entries.delete(docId);
2503
- }
2504
- async getDocSnapshot(docId) {
2505
- return this.entries.get(docId)?.snapshot ?? null;
2506
- }
2507
- async setDocSnapshot(snapshot) {
2508
- const entry = this.entry(snapshot.docId);
2509
- if (entry.snapshot == null || entry.snapshot.updatedAt <= snapshot.updatedAt) entry.snapshot = snapshot;
2510
- return true;
2511
- }
2512
- async getDocUpdates(docId) {
2513
- return [...this.entries.get(docId)?.updates ?? []];
2514
- }
2515
- async markUpdatesMerged(docId, updates) {
2516
- const entry = this.entries.get(docId);
2517
- if (!entry) return 0;
2518
- const merged = new Set(updates.map((update) => update.seq));
2519
- entry.updates = entry.updates.filter((update) => !merged.has(update.seq));
2520
- return merged.size;
2521
- }
2522
- entry(docId) {
2523
- let entry = this.entries.get(docId);
2524
- if (!entry) {
2525
- entry = {
2526
- snapshot: null,
2527
- updates: [],
2528
- seq: 0
2529
- };
2530
- this.entries.set(docId, entry);
54
+ return mutation();
55
+ } finally {
56
+ this.running = false;
2531
57
  }
2532
- return entry;
2533
58
  }
2534
- now() {
2535
- return /* @__PURE__ */ new Date();
59
+ close() {
60
+ this.closed = true;
2536
61
  }
2537
62
  };
2538
63
  //#endregion
@@ -2648,9 +173,9 @@ var DocumentUndoManager = class {
2648
173
  }
2649
174
  };
2650
175
  //#endregion
2651
- //#region src/session/mengine-doc-session.ts
176
+ //#region src/session/document-session.ts
2652
177
  /**
2653
- * Raised by {@link MengineDocSession.waitForServerAck} when the local edits it
178
+ * Raised by {@link DocumentSession.waitForServerAck} when the local edits it
2654
179
  * was asked to confirm were not acknowledged as durable.
2655
180
  *
2656
181
  * Named `...AckFailed`, not `...AckTimeout`: two of the three `reason` values are
@@ -2679,26 +204,10 @@ var MengineAckFailedError = class extends Error {
2679
204
  this.name = "MengineAckFailedError";
2680
205
  }
2681
206
  };
2682
- /**
2683
- * A live editing session for one Medeo document — the single entry point clients
2684
- * (FE draft driver, Agent, embeds) use to open, edit, and observe a document.
2685
- *
2686
- * SemanticEditor writes through MirrorVideoDocumentAdapter directly, while
2687
- * DocumentUndoManager observes the same LoroDoc independently. Both mutation
2688
- * paths use the session's DocumentMutationGuard, closed before teardown.
2689
- *
2690
- * `DocManager` owns the `LoroDoc`: local edits committed on the adapter are
2691
- * picked up via `subscribeLocalUpdates`, saved to local storage, then pushed to
2692
- * the server by the synchronizer. Remote SSE updates flow server → synchronizer
2693
- * → local storage → manager → `LoroDoc`. A single `LoroDoc.subscribe` turns any
2694
- * resulting change into a snapshot event, so callers never track update ids by
2695
- * hand.
2696
- *
2697
- * Replaces the per-consumer hand-written pull/SSE loops that previously lived in
2698
- * the standalone client session and `MengineDraftDriver` with one verified path.
2699
- */
2700
- var MengineDocSession = class {
207
+ /** Shared binary synchronization lifecycle. Models attach only after initial loading succeeds. */
208
+ var DocumentSession = class {
2701
209
  options;
210
+ model;
2702
211
  docId;
2703
212
  local;
2704
213
  server;
@@ -2706,7 +215,7 @@ var MengineDocSession = class {
2706
215
  manager;
2707
216
  events = new EventBus();
2708
217
  disposables = new DisposableSet();
2709
- mutationGuard = new DocumentMutationGuard();
218
+ mutationGuard;
2710
219
  adapterValue = null;
2711
220
  editorValue = null;
2712
221
  undoManagerValue = null;
@@ -2724,14 +233,17 @@ var MengineDocSession = class {
2724
233
  * doc (e.g. nothing was edited since the last confirmed push).
2725
234
  */
2726
235
  serverVVValue = null;
2727
- constructor(options) {
236
+ constructor(options, model) {
2728
237
  this.options = options;
238
+ this.model = model;
239
+ this.mutationGuard = new DocumentMutationGuard(options.readonlyMode);
2729
240
  this.docId = options.docId;
2730
241
  this.local = options.localStorage ?? new MemoryDocStorage();
2731
242
  this.server = new MedeoHttpDocStorage({
2732
243
  docId: options.docId,
2733
244
  client: options.client,
2734
- sseReconnectDelayMs: options.sseReconnectDelayMs
245
+ sseReconnectDelayMs: options.sseReconnectDelayMs,
246
+ readonlyMode: options.readonlyMode
2735
247
  });
2736
248
  this.synchronizer = new ClientServerSynchronizer(this.local, this.server);
2737
249
  this.manager = new DocManager(this.local, this.synchronizer);
@@ -2814,6 +326,19 @@ var MengineDocSession = class {
2814
326
  if (this.editorValue == null) throw new Error("mengine doc session is not started");
2815
327
  return this.editorValue;
2816
328
  }
329
+ /**
330
+ * Opaque version token of the local oplog (base64 `VersionVector.encode`).
331
+ * Equality-comparable only: equal means no observed change (local or
332
+ * remote-arrived) since the token was taken. Throws when not started.
333
+ */
334
+ version() {
335
+ if (this.adapterValue == null) throw new Error("mengine doc session is not started");
336
+ return bytesToBase64(this.adapterValue.doc.oplogVersion().encode());
337
+ }
338
+ get document() {
339
+ if (this.adapterValue == null || this.editorValue == null) throw new Error("mengine doc session is not started");
340
+ return this.adapterValue.document;
341
+ }
2817
342
  /** Current document snapshot (read model). */
2818
343
  snapshot() {
2819
344
  if (this.adapterValue == null) throw new Error("mengine doc session is not started");
@@ -2921,26 +446,29 @@ var MengineDocSession = class {
2921
446
  const doc = this.manager.connectDoc(this.docId);
2922
447
  this.disposables.add(() => this.manager.disconnectDoc(this.docId));
2923
448
  if (this.options.peerId != null) doc.setPeerId(this.options.peerId);
2924
- this.adapterValue = new MirrorVideoDocumentAdapter(doc, this.mutationGuard);
2925
- const unsubscribe = doc.subscribe((event) => {
2926
- if (this.adapterValue == null) return;
2927
- if (event.by === "local") this.localVersionValue = doc.version();
2928
- this.emitSyncState();
2929
- this.events.emit("update", {
2930
- source: event.by === "local" ? "local" : "remote",
2931
- snapshot: this.adapterValue.snapshot()
2932
- });
2933
- });
2934
- this.disposables.add(unsubscribe);
2935
449
  this.disposables.add(this.manager.onDocStateChange(this.docId, (state) => {
2936
450
  this.events.emit("state", state);
2937
451
  this.emitSyncState();
2938
452
  }));
2939
453
  await this.waitForInitialLoad();
2940
454
  if (this.destroyed) throw new Error("mengine doc session destroyed");
2941
- assertValidVideoDocument(this.snapshot());
455
+ this.adapterValue = this.model(doc, this.mutationGuard);
456
+ this.disposables.add(this.adapterValue.subscribe((source) => {
457
+ if (!this.adapterValue) return;
458
+ if (source === "local") this.localVersionValue = doc.version();
459
+ this.emitSyncState();
460
+ this.events.emit("update", {
461
+ source,
462
+ snapshot: this.adapterValue.snapshot()
463
+ });
464
+ }));
465
+ this.events.emit("update", {
466
+ source: "remote",
467
+ snapshot: this.snapshot()
468
+ });
469
+ if (this.destroyed) throw new Error("mengine doc session destroyed");
2942
470
  this.undoManagerValue = new DocumentUndoManager(doc, this.mutationGuard, () => this.events.emit("undoState"));
2943
- this.editorValue = new SemanticEditor(this.adapterValue);
471
+ this.editorValue = this.adapterValue.editor;
2944
472
  this.initialVersionValue = doc.version();
2945
473
  this.emitSyncState();
2946
474
  return this.snapshot();
@@ -3042,4 +570,29 @@ function serverCovers(serverVV, target) {
3042
570
  return covers(server, target);
3043
571
  }
3044
572
  //#endregion
3045
- 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, 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 };
573
+ //#region src/session/mengine-doc-session.ts
574
+ /** Default collaborative session: one DSL handle and one editor over the synchronized LoroDoc. */
575
+ var MengineDocSession = class extends DocumentSession {
576
+ constructor(options) {
577
+ super(options, (doc, guard) => {
578
+ assertDslDocument(doc);
579
+ const dsl = new MedeoDsl(doc, { mutationGuard: guard });
580
+ return {
581
+ doc,
582
+ document: dsl,
583
+ editor: new DslEditor(dsl),
584
+ snapshot: () => dsl.getSnapshot(),
585
+ subscribe: (listener) => dsl.subscribe(listener),
586
+ dispose: () => dsl.dispose()
587
+ };
588
+ });
589
+ }
590
+ get dsl() {
591
+ return this.document;
592
+ }
593
+ projectVideoDraft() {
594
+ return toVideoDraft(this.snapshot());
595
+ }
596
+ };
597
+ //#endregion
598
+ export { DEFAULT_UNIT_TIME_MS, DslEditor, DslProjectionError, ManualSyncDoc, MedeoDsl, MedeoHttpDocStorage, MemoryDocStorage, MengineAckFailedError, MengineDocSession, MengineHttpClient, MengineHttpRequestError, MenginePushRejectedError, UnsupportedDocumentFormatError, assertDslDocument, base64ToBytes, bytesToBase64, captionContentCodec, createInitialMedeoDsl, decodeDocVersionMark, defaultIdFactory, documentFormat, encodeDocVersionMark, fromVideoDraft, isInvalidAtomic, medeoDslLoroShape, readMengineEventStream, relationId, resolveDslLayout, resourceId, timeAnchorCodec, toVideoDraft, tupleHash };