@mengine/medeo-tool 1.3.1-alpha.9 → 1.4.1-alpha.1

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.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { c as renderPreview, l as renderCompactProjection, n as EntitySandbox, o as isMediaAssetVariantKind, r as toDslRows, s as collectAffectedPartIds, t as EditSandboxSession } from "./script-session-B2UQ1W_y.mjs";
2
- import { EntityGraphHttpClient, ManualSyncDoc, MengineHttpClient, MengineHttpRequestError, ValidationError, createMirrorVideoDocument, createPlainMemoryAdapter, decodeDocVersionMark, encodeDocVersionMark, migrateLegacyTimelineToEntities, replayJournal, toVideoDocument } from "@mengine/medeo-client";
1
+ import { a as createEntityId, c as collectAffectedPartIds, i as businessState, l as renderPreview, n as EntitySandbox, o as createRelationId, s as isMediaAssetVariantKind, t as EditSandboxSession, u as renderCompactProjection } from "./script-session-AukLN7x7.mjs";
2
+ import { LoroEntityDocument, ManualSyncDoc, MengineHttpClient, MengineHttpRequestError, ValidationError, base64ToBytes, bytesToBase64, compileEntityRows, createMirrorVideoDocument, createPlainMemoryAdapter, decodeDocVersionMark, encodeDocVersionMark, ensureEditorFoundation, replayJournal, toVideoDocument } from "@mengine/medeo-client";
3
3
  import { Worker } from "node:worker_threads";
4
- import { randomUUID } from "node:crypto";
4
+ import { createHash, randomUUID } from "node:crypto";
5
5
  //#region src/sandbox/node-host.ts
6
6
  const DEFAULT_TIMEOUT_MS = 2e3;
7
7
  const DEFAULT_MEMORY_MB = 256;
@@ -122,6 +122,7 @@ function runEditScript(options) {
122
122
  doc_id: options.document.meta.draft_id ?? "",
123
123
  base_version: options.baseVersion,
124
124
  ops: ops.slice(),
125
+ ...message.loroUpdate ? { loro_update: message.loroUpdate } : {},
125
126
  entity_base_revision: message.entityBaseRevision,
126
127
  entity_commands: entityCommands.slice(),
127
128
  ...message.entityRows !== void 0 ? { entity_rows: message.entityRows } : {},
@@ -179,6 +180,78 @@ function runEditScript(options) {
179
180
  });
180
181
  }
181
182
  //#endregion
183
+ //#region src/entity/caption-asset-assembly.ts
184
+ /** Assemble optional infrastructure after business execution, using exact entity identity. */
185
+ async function assembleCaptionAssets(input) {
186
+ try {
187
+ const state = await input.client.fetchState();
188
+ const candidates = state.entities.filter((entity) => entity.entity_kind === "caption" && !state.relations.some((relation) => relation.relation_kind === "physical-asset" && (relation.endpoint_0_entity_id === entity.entity_id || relation.endpoint_1_entity_id === entity.entity_id)));
189
+ if (!candidates.length) return { status: "current" };
190
+ const ids = new Set(candidates.map((entity) => entity.entity_id));
191
+ const facts = await input.loadAssets(input.docId, [...ids]);
192
+ const entities = [...state.entities];
193
+ const relations = [...state.relations];
194
+ const bound = /* @__PURE__ */ new Map();
195
+ for (const fact of facts) {
196
+ if (!ids.has(fact.captionEntityId) || !trimmed(fact.assetId) || !trimmed(fact.storageKey)) throw new Error("Caption Asset fact must identify a requested Caption and a real Asset locator");
197
+ const previous = bound.get(fact.captionEntityId);
198
+ if (previous !== void 0) {
199
+ if (previous !== fact.assetId) throw new Error(`Conflicting Assets for Caption ${fact.captionEntityId}`);
200
+ continue;
201
+ }
202
+ bound.set(fact.captionEntityId, fact.assetId);
203
+ const matches = entities.filter((entity) => {
204
+ const external = entity.payload.external;
205
+ return external !== null && typeof external === "object" && !Array.isArray(external) && external.system === "memota" && external.key === fact.assetId;
206
+ });
207
+ if (matches.length > 1 || matches[0] && matches[0].entity_kind !== "asset") throw new Error(`Conflicting resource identity for Caption Asset ${fact.assetId}`);
208
+ let asset = matches[0];
209
+ if (asset && asset.payload.storageKey !== fact.storageKey) throw new Error(`Conflicting storage key for Caption Asset ${fact.assetId}`);
210
+ if (!asset) {
211
+ asset = {
212
+ entity_id: stableId$1("asset", fact.assetId),
213
+ entity_kind: "asset",
214
+ payload: {
215
+ external: {
216
+ system: "memota",
217
+ key: fact.assetId
218
+ },
219
+ storageKey: fact.storageKey
220
+ }
221
+ };
222
+ if (entities.some((entity) => entity.entity_id === asset.entity_id)) throw new Error("Caption Asset identity collision");
223
+ entities.push(asset);
224
+ }
225
+ relations.push({
226
+ relation_id: stableId$1("relation", fact.captionEntityId, asset.entity_id),
227
+ relation_kind: "physical-asset",
228
+ endpoint_0_entity_id: fact.captionEntityId,
229
+ endpoint_1_entity_id: asset.entity_id,
230
+ metadata: {},
231
+ trace: {}
232
+ });
233
+ }
234
+ if (!bound.size) return { status: "current" };
235
+ await input.client.commit(state.revision, {
236
+ ...state,
237
+ entities,
238
+ relations
239
+ });
240
+ return { status: "applied" };
241
+ } catch (error) {
242
+ return {
243
+ status: "failed",
244
+ message: error instanceof Error ? error.message : String(error)
245
+ };
246
+ }
247
+ }
248
+ function stableId$1(prefix, ...parts) {
249
+ return `${prefix}_${createHash("sha256").update(JSON.stringify(parts)).digest("hex")}`;
250
+ }
251
+ function trimmed(value) {
252
+ return typeof value === "string" && value.length > 0 && value.trim() === value;
253
+ }
254
+ //#endregion
182
255
  //#region src/entity/entity-contract.ts
183
256
  const KNOWN_ENTITY_KINDS = [
184
257
  "axvideo",
@@ -226,7 +299,7 @@ var MengineEntityHttpRequestError = class extends Error {
226
299
  this.name = "MengineEntityHttpRequestError";
227
300
  }
228
301
  };
229
- /** Narrow authenticated client for the entity-store CAS endpoint. */
302
+ /** Narrow authenticated client for the Loro entity endpoint. */
230
303
  var EntityHttpClient = class {
231
304
  options;
232
305
  fetchImpl;
@@ -237,18 +310,30 @@ var EntityHttpClient = class {
237
310
  async fetchState() {
238
311
  return toSnapshot(await this.requestJson({ method: "GET" }), this.options.docId);
239
312
  }
240
- async commit(expectedRevision, state, deletions = {}) {
313
+ async commit(_transportSequence, state, _deletions = {}) {
314
+ if (!state.loroSnapshot) throw new Error("Entity edit is missing its causal Loro baseline");
315
+ const rows = {
316
+ entities: state.entities.map((row) => ({
317
+ entityId: createEntityId(row.entity_id),
318
+ entityKind: row.entity_kind,
319
+ payload: row.payload
320
+ })),
321
+ relations: state.relations.map((row) => ({
322
+ relationId: createRelationId(row.relation_id),
323
+ relationKind: row.relation_kind,
324
+ endpoint0EntityId: createEntityId(row.endpoint_0_entity_id),
325
+ endpoint1EntityId: createEntityId(row.endpoint_1_entity_id),
326
+ metadata: row.metadata,
327
+ trace: row.trace
328
+ }))
329
+ };
330
+ const compiled = compileEntityRows(base64ToBytes(state.loroSnapshot), rows);
331
+ return this.commitUpdate(bytesToBase64(compiled.update));
332
+ }
333
+ async commitUpdate(update) {
241
334
  return toSnapshot(await this.requestJson({
242
335
  method: "POST",
243
- body: JSON.stringify({
244
- expected_revision: expectedRevision,
245
- rows: {
246
- entities: state.entities,
247
- relations: state.relations
248
- },
249
- deleted_entity_ids: [...deletions.deleted_entity_ids ?? []],
250
- deleted_relation_ids: [...deletions.deleted_relation_ids ?? []]
251
- })
336
+ body: JSON.stringify({ update })
252
337
  }), this.options.docId);
253
338
  }
254
339
  async requestJson(init) {
@@ -279,12 +364,13 @@ function toSnapshot(value, expectedDocId) {
279
364
  if (!isRecord$2(value) || typeof value.doc_id !== "string" || !isNonNegativeInteger(value.revision)) throw new Error("invalid entity-state response envelope");
280
365
  if (value.doc_id !== expectedDocId) throw new Error(`entity-state response doc_id mismatch: expected "${expectedDocId}"`);
281
366
  if (!isRecord$2(value.rows) || !Array.isArray(value.rows.entities) || !Array.isArray(value.rows.relations)) throw new Error("invalid entity-state response rows");
282
- if (value.audio_script_entity_id === null) {
283
- if (value.revision !== 0) throw new Error("Document AudioScript is not initialized");
284
- } else if (!isTrimmed(value.audio_script_entity_id)) throw new Error("invalid document AudioScript identity");
367
+ if (value.audio_script_entity_id === null) throw new Error("Document AudioScript is not initialized");
368
+ if (!isTrimmed(value.audio_script_entity_id)) throw new Error("invalid document AudioScript identity");
369
+ if (typeof value.loro_snapshot !== "string") throw new Error("Missing causal Loro snapshot");
285
370
  const response = value;
286
371
  if (response.audio_script_entity_id !== null && !response.rows.entities.some((entity) => entity.entity_id === response.audio_script_entity_id && entity.entity_kind === "audio-script")) throw new Error("Document AudioScript must name the project AudioScript");
287
372
  return {
373
+ loroSnapshot: response.loro_snapshot,
288
374
  revision: response.revision,
289
375
  audioScriptEntityId: response.audio_script_entity_id,
290
376
  entities: response.rows.entities.map(parseEntity),
@@ -338,8 +424,6 @@ async function safeReadJson(response) {
338
424
  * Voice results use the speech system; every other medium uses `memota`.
339
425
  */
340
426
  const ASSET_SYSTEMS = new Set(["memota", "memota-speech"]);
341
- /** Bounded CAS retry budget for the sync commit after a concurrent winner. */
342
- const MAX_COMMIT_ATTEMPTS = 3;
343
427
  /** Validate host-supplied facts; a malformed record fails the whole query. */
344
428
  function parseGenerationFacts(value) {
345
429
  if (!Array.isArray(value)) throw new Error("generation facts must be an array");
@@ -414,49 +498,33 @@ function planGeneratedRelations(input) {
414
498
  * Sync generation lineage after a confirmed entity commit. Any failure is
415
499
  * returned as a `failed` outcome instead of thrown, so the already-durable
416
500
  * commit result is never masked; a successful query that finds nothing is
417
- * `current`. Asset identities are immutable, so facts are queried once. A
418
- * revision conflict re-reads current entities and relations, re-plans, and
419
- * retries within `MAX_COMMIT_ATTEMPTS`; deleted endpoints are never recreated.
501
+ * `current`. Asset identities are immutable, so facts are queried once. The native update retains its causal baseline and merges without whole-state retries; deleted endpoints are never recreated.
420
502
  */
421
503
  async function syncGeneratedRelations(input) {
422
504
  const { client, docId, baseState, entityCommands, loadFacts } = input;
423
505
  try {
424
- let state = await client.fetchState();
425
- let scope = planGenerationScope(baseState, entityCommands, state);
506
+ const state = await client.fetchState();
507
+ const scope = planGenerationScope(baseState, entityCommands, state);
426
508
  if (scope.queryAssetKeys.length === 0) return { status: "current" };
427
509
  const facts = parseGenerationFacts(await loadFacts(docId, scope.queryAssetKeys));
428
- for (let attempt = 1; attempt <= MAX_COMMIT_ATTEMPTS; attempt++) {
429
- if (scope.queryAssetKeys.length === 0) return { status: "current" };
430
- const relations = planGeneratedRelations({
431
- baseState,
432
- state,
433
- scopedMediaIds: scope.scopedMediaIds,
434
- facts,
435
- newRelationId: mintRelationId
436
- });
437
- if (relations.length === 0) return { status: "current" };
438
- try {
439
- await client.commit(state.revision, {
440
- ...state,
441
- relations: [...state.relations, ...relations]
442
- });
443
- return {
444
- status: "applied",
445
- created_relation_ids: relations.map((relation) => relation.relation_id)
446
- };
447
- } catch (error) {
448
- if (!(error instanceof MengineEntityHttpRequestError && error.status === 409) || attempt === MAX_COMMIT_ATTEMPTS) return {
449
- status: "failed",
450
- message: `generation lineage sync commit failed: ${errorMessage(error)}`
451
- };
452
- state = await client.fetchState();
453
- scope = planGenerationScope(baseState, entityCommands, state);
454
- }
455
- }
456
- return {
457
- status: "failed",
458
- message: "generation lineage sync exhausted its retry budget"
459
- };
510
+ const relations = planGeneratedRelations({
511
+ baseState,
512
+ state,
513
+ scopedMediaIds: scope.scopedMediaIds,
514
+ facts,
515
+ newRelationId: mintRelationId
516
+ });
517
+ if (relations.length === 0) return { status: "current" };
518
+ const committed = await client.commit(state.revision, {
519
+ ...state,
520
+ relations: [...state.relations, ...relations]
521
+ });
522
+ const active = new Set(committed.relations.map((relation) => relation.relation_id));
523
+ const created = relations.map((relation) => relation.relation_id).filter((id) => active.has(id));
524
+ return created.length ? {
525
+ status: "applied",
526
+ created_relation_ids: created
527
+ } : { status: "current" };
460
528
  } catch (error) {
461
529
  return {
462
530
  status: "failed",
@@ -513,86 +581,10 @@ function isRecord$1(value) {
513
581
  return value !== null && typeof value === "object" && !Array.isArray(value);
514
582
  }
515
583
  //#endregion
516
- //#region src/migration-input.ts
517
- /** Validate host-recalled facts without accepting a caller-controlled snapshot or VV. */
518
- function parseMigrationAssetFacts(value) {
519
- if (!Array.isArray(value)) throw new Error("asset_facts is required for migrate-legacy and must be an array");
520
- return value.map((item) => {
521
- if (!record(item)) throw new Error("Each asset_facts entry must be an object");
522
- const { assetId, kind, durationMs, storageKey, voice } = item;
523
- if (!nonempty(assetId)) throw new Error("asset_facts.assetId must be a non-empty trimmed string");
524
- if (kind !== "image" && kind !== "video" && kind !== "audio" && kind !== "voice") throw new Error("asset_facts.kind must be image, video, audio, or voice");
525
- if (Object.keys(item).some((key) => ![
526
- "assetId",
527
- "kind",
528
- "durationMs",
529
- "storageKey",
530
- "voice"
531
- ].includes(key))) throw new Error("Unknown asset_facts field");
532
- if (storageKey !== void 0 && !nonempty(storageKey)) throw new Error("asset_facts.storageKey must be non-empty");
533
- if (kind === "image") {
534
- if (durationMs !== void 0 || voice !== void 0) throw new Error("Image facts cannot declare duration or voice");
535
- return {
536
- assetId,
537
- kind,
538
- ...storageKey === void 0 ? {} : { storageKey }
539
- };
540
- }
541
- if (typeof durationMs !== "number" || !Number.isSafeInteger(durationMs) || durationMs <= 0) throw new Error("asset_facts.durationMs must be factual positive whole milliseconds");
542
- if (kind === "video") {
543
- if (voice !== void 0) throw new Error("Video facts cannot declare voice");
544
- return {
545
- assetId,
546
- kind,
547
- durationMs,
548
- ...storageKey === void 0 ? {} : { storageKey }
549
- };
550
- }
551
- if (!nonempty(storageKey)) throw new Error("Audio and Voice facts require their physical storageKey");
552
- if (kind === "audio") {
553
- if (voice !== void 0) throw new Error("Audio facts cannot declare a Voice descriptor");
554
- return {
555
- assetId,
556
- kind,
557
- durationMs,
558
- storageKey
559
- };
560
- }
561
- if (!record(voice) || voice.system !== "voice-library" || !nonempty(voice.key) || voice.name !== void 0 && typeof voice.name !== "string" || Object.keys(voice).some((key) => ![
562
- "system",
563
- "key",
564
- "name"
565
- ].includes(key))) throw new Error("Voice facts require an explicit voice-library descriptor");
566
- return {
567
- assetId,
568
- kind,
569
- durationMs,
570
- storageKey,
571
- voice: {
572
- system: "voice-library",
573
- key: voice.key,
574
- ...voice.name === void 0 ? {} : { name: voice.name }
575
- }
576
- };
577
- });
578
- }
579
- function record(value) {
580
- return value !== null && typeof value === "object" && !Array.isArray(value);
581
- }
582
- function nonempty(value) {
583
- return typeof value === "string" && value.length > 0 && value.trim() === value;
584
- }
585
- //#endregion
586
584
  //#region src/sandbox/generated/entity-edit-sandbox-model-context.ts
587
585
  /** @generated by gen:sandbox-dts. DO NOT EDIT. */
588
586
  const ENTITY_EDIT_SANDBOX_API_DTS = [
589
587
  "/** @generated by gen:sandbox-dts. Entity-native editor contract; DO NOT EDIT. */",
590
- "export interface AudioMediaAssetFact {",
591
- " readonly assetId: string;",
592
- " readonly kind: 'audio';",
593
- " readonly durationMs: number;",
594
- " readonly storageKey: string;",
595
- "}",
596
588
  "export interface BoundedDerivedSequencePayload extends JsonObject {",
597
589
  " extent: {",
598
590
  " kind: 'bounded';",
@@ -612,6 +604,42 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
612
604
  " sampling: 'native';",
613
605
  " coordinateSpace: JsonValue;",
614
606
  "}",
607
+ "/** Business editing surface. Infrastructure Assets are assembled by the host. */",
608
+ "export interface BusinessEntityFacade {",
609
+ " list(): SandboxEntity[];",
610
+ " get(entityId: string): SandboxEntity | null;",
611
+ " readCaptionContent(entityId: string): ComposedScriptContent;",
612
+ " readPhoneticScriptContent(entityId: string): ComposedPhoneticContent;",
613
+ " create(",
614
+ " input: Exclude<",
615
+ " CreateEntityInput,",
616
+ " {",
617
+ " entity_kind: 'asset';",
618
+ " }",
619
+ " >,",
620
+ " ): string;",
621
+ " update(input: UpdateEntityInput): void;",
622
+ " declareFields(input: UpdateEntityInput): void;",
623
+ " delete(input: DeleteEntityInput): void;",
624
+ "}",
625
+ "/** Physical Asset bindings are maintained outside the sandbox. */",
626
+ "export interface BusinessRelationFacade {",
627
+ " list(): SandboxRelation[];",
628
+ " of(entityId: string, relationKind?: KnownRelationKind): SandboxRelation[];",
629
+ " link(",
630
+ " input: Exclude<",
631
+ " LinkRelationInput,",
632
+ " {",
633
+ " relation_kind: 'physical-asset';",
634
+ " }",
635
+ " >,",
636
+ " ): string;",
637
+ " linkGenerated(input: LinkGeneratedRelationInput): string;",
638
+ " linkClipAnchor(input: LinkClipAnchorRelationInput): string;",
639
+ " linkPhoneticScriptRender(input: LinkPhoneticScriptRenderRelationInput): string;",
640
+ " linkAudioScriptSource(input: LinkAudioScriptSourceRelationInput): string;",
641
+ " unlink(input: UnlinkRelationInput): void;",
642
+ "}",
615
643
  "export interface CaptionFontDescriptor {",
616
644
  " readonly system: 'font-library';",
617
645
  " readonly key: string;",
@@ -714,28 +742,6 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
714
742
  " | 'axvideo-marker'",
715
743
  " | 'marker-timeline'",
716
744
  " | 'audio-script-marker';",
717
- "export interface EntityFacade {",
718
- " /** Read complete assembled fields; returned objects are snapshots. Use update to persist edits. */",
719
- " list(): SandboxEntity[];",
720
- " get(entityId: string): SandboxEntity | null;",
721
- " /** Find document resources by external Memota asset id, including directly composed media variants. */",
722
- " findByAssetId(assetId: string): SandboxEntity<ResourceEntityKind>[];",
723
- " /** Assemble selected Caption text; missing composition is an error. */",
724
- " readCaptionContent(entityId: string): ComposedScriptContent;",
725
- " /** Assemble base text and pronunciation fields before generating Voice. */",
726
- " readPhoneticScriptContent(entityId: string): ComposedPhoneticContent;",
727
- " create(input: CreateEntityInput): string;",
728
- " /** Patch assembled fields, routing inherited fields to their declaring entity. */",
729
- " update(input: UpdateEntityInput): void;",
730
- " /** Explicitly declare own fields, overriding unambiguous bases without modifying them. Ordinary edits use update. */",
731
- " declareFields(input: UpdateEntityInput): void;",
732
- " /** Delete an Entity only after all of its incident Relations have been explicitly unlinked. */",
733
- " delete(input: DeleteEntityInput): void;",
734
- " /** Get or create one typed Asset by factual external id and return its single content identity. Never creates a Clip. */",
735
- " ensureMedia(fact: MediaAssetFact): {",
736
- " contentEntityId: string;",
737
- " };",
738
- "}",
739
745
  "export type EntityId = string;",
740
746
  "export interface EntityPayloadByKind {",
741
747
  " axvideo: BoundedDerivedSequencePayload;",
@@ -745,12 +751,10 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
745
751
  " role?: string;",
746
752
  " };",
747
753
  " clip: JsonObject;",
748
- " /** Physical resource fields; never a copy of Caption content. */",
749
- " asset: JsonObject;",
750
- " video: BoundedNativeSequencePayload & MediaAssetPayload;",
751
- " audio: BoundedNativeSequencePayload & MediaAssetPayload;",
752
- " voice: BoundedNativeSequencePayload & MediaAssetPayload;",
753
- " image: UnboundedConstantSequencePayload & MediaAssetPayload;",
754
+ " video: BoundedNativeSequencePayload;",
755
+ " audio: BoundedNativeSequencePayload;",
756
+ " voice: BoundedNativeSequencePayload;",
757
+ " image: UnboundedConstantSequencePayload;",
754
758
  " 'sequence-marker': JsonObject & {",
755
759
  " sourceRange: {",
756
760
  " start: number;",
@@ -794,19 +798,18 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
794
798
  " };",
795
799
  "}",
796
800
  "export interface EntityStoreSnapshot {",
801
+ " /** Causal compiler baseline; required for publishing edits. */",
802
+ " loroSnapshot?: string;",
797
803
  " revision: number;",
798
- " /** Fixed identity outlet of the project's one AudioScript; null only before initialization (revision 0). */",
804
+ " /** Current AudioScript version attached to the project; initialized projects always attach a script, possibly empty. */",
799
805
  " audioScriptEntityId: string | null;",
800
806
  " entities: SandboxEntity[];",
801
807
  " relations: SandboxRelation[];",
802
808
  "}",
803
- "export interface ImageMediaAssetFact {",
804
- " readonly assetId: string;",
805
- " readonly kind: 'image';",
806
- " readonly storageKey?: string;",
807
- "}",
808
809
  "export interface InsertCaptionClipInput {",
809
810
  " readonly timelineEntityId: string;",
811
+ " /** Existing generation identity for newly materialized Caption content, distinct from its Clip. */",
812
+ " readonly captionEntityId?: string;",
810
813
  " /** Stable placed caption identity, distinct from the Caption content identity. */",
811
814
  " readonly captionClipEntityId?: string;",
812
815
  " /** Existing bases composed by this variant; includes an AudioScript text owner. */",
@@ -827,21 +830,6 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
827
830
  " readonly targetRange?: SequenceRange<number>;",
828
831
  " readonly clipPayload?: JsonObject;",
829
832
  "}",
830
- "export interface InsertMediaClipInput {",
831
- " readonly timelineEntityId: string;",
832
- " readonly clipEntityId?: string;",
833
- " readonly media: VisualMediaAssetFact;",
834
- " /** Source/display window in whole milliseconds. Images use this as their finite display span. */",
835
- " readonly sourceRange: SequenceRange<number>;",
836
- " readonly placement: ClipPlacement;",
837
- " readonly volume?: number;",
838
- "}",
839
- "export interface InsertMediaClipsInput {",
840
- " readonly timelineEntityId: string;",
841
- " readonly clips: readonly ReplacementMediaClipInput[];",
842
- " /** One placement decision for the whole input-ordered block. */",
843
- " readonly insertion: MediaClipInsertion;",
844
- "}",
845
833
  "export interface InsertPlacedClipInput {",
846
834
  " readonly trackEntityId: string;",
847
835
  " readonly contentEntityId: string;",
@@ -862,7 +850,6 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
862
850
  " | 'timeline'",
863
851
  " | 'track'",
864
852
  " | 'clip'",
865
- " | 'asset'",
866
853
  " | 'video'",
867
854
  " | 'audio'",
868
855
  " | 'voice'",
@@ -879,7 +866,6 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
879
866
  " | 'marker-content'",
880
867
  " | 'axvideo-marker'",
881
868
  " | 'marker-timeline'",
882
- " | 'physical-asset'",
883
869
  " | 'generated'",
884
870
  " | 'caption-alignment'",
885
871
  " | 'clip-anchor'",
@@ -939,28 +925,6 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
939
925
  " alignment: JsonValue;",
940
926
  " };",
941
927
  " });",
942
- "/** Facts resolved from media storage. A trim window never substitutes for intrinsic duration. */",
943
- "export type MediaAssetFact = ImageMediaAssetFact | VideoMediaAssetFact | AudioMediaAssetFact | VoiceMediaAssetFact;",
944
- "export type MediaAssetPayload = JsonObject & {",
945
- " external: {",
946
- " system: 'memota' | 'memota-speech';",
947
- " key: string;",
948
- " };",
949
- " storageKey?: string;",
950
- "};",
951
- "export type MediaClipInsertion =",
952
- " | {",
953
- " readonly kind: 'before';",
954
- " readonly clipEntityId: string;",
955
- " }",
956
- " | {",
957
- " readonly kind: 'after';",
958
- " readonly clipEntityId: string;",
959
- " }",
960
- " | {",
961
- " readonly kind: 'firstStart';",
962
- " readonly startMs: number;",
963
- " };",
964
928
  "export interface MoveClipInput {",
965
929
  " readonly clipEntityId: string;",
966
930
  " readonly trackEntityId: string;",
@@ -987,23 +951,6 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
987
951
  " readonly timelineEntityId: string;",
988
952
  " readonly style: CaptionStyleFields;",
989
953
  "}",
990
- "export interface RelationFacade {",
991
- " list(): SandboxRelation[];",
992
- " /** Incident lookup is endpoint-agnostic; persisted endpoint positions stay unchanged. */",
993
- " of(entityId: string, relationKind?: KnownRelationKind): SandboxRelation[];",
994
- " /** Link existing entities through ordinary associations; variant bases are stored directly on the variant. */",
995
- " link(input: LinkRelationInput): string;",
996
- " /** Author ordered generated(output,input); generic link() deliberately rejects this kind. */",
997
- " linkGenerated(input: LinkGeneratedRelationInput): string;",
998
- " /** Author ordered clip-anchor(child,host) without positional endpoint ambiguity. */",
999
- " linkClipAnchor(input: LinkClipAnchorRelationInput): string;",
1000
- " /** Author ordered phonetic-script-render(output,script) without positional endpoint ambiguity. */",
1001
- " linkPhoneticScriptRender(input: LinkPhoneticScriptRenderRelationInput): string;",
1002
- " /** Author ordered audio-script-source(script,source) without positional endpoint ambiguity. */",
1003
- " linkAudioScriptSource(input: LinkAudioScriptSourceRelationInput): string;",
1004
- " /** Remove a Relation by identity; endpoint replacement is an explicit unlink plus link. */",
1005
- " unlink(input: UnlinkRelationInput): void;",
1006
- "}",
1007
954
  "export interface ReplaceClipContentInput {",
1008
955
  " readonly clipEntityId: string;",
1009
956
  " /** Existing Sequence media Entity id. Asset ids and URLs are not content ids. */",
@@ -1013,25 +960,6 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
1013
960
  " readonly targetRange?: SequenceRange<number>;",
1014
961
  " readonly timeRemapping?: JsonValue;",
1015
962
  "}",
1016
- "export interface ReplaceMediaClipInput {",
1017
- " readonly clipEntityId: string;",
1018
- " readonly media: VisualMediaAssetFact;",
1019
- " readonly sourceRange: SequenceRange<number>;",
1020
- "}",
1021
- "export interface ReplaceSequentialClipsInput {",
1022
- " readonly timelineEntityId: string;",
1023
- " readonly oldClipEntityIds: readonly string[];",
1024
- " readonly newClips: readonly ReplacementMediaClipInput[];",
1025
- " readonly onAnchored: 'remap' | 'cascade';",
1026
- "}",
1027
- "export interface ReplacementMediaClipInput {",
1028
- " readonly clipEntityId?: string;",
1029
- " readonly media: VisualMediaAssetFact;",
1030
- " readonly sourceRange: SequenceRange<number>;",
1031
- " readonly volume?: number;",
1032
- "}",
1033
- "/** Asset identity, either an old physical-only row or a directly composed media variant. */",
1034
- "export type ResourceEntityKind = 'image' | 'video' | 'audio' | 'voice';",
1035
963
  "export interface SandboxEntity<K extends KnownEntityKind = KnownEntityKind> {",
1036
964
  " entity_id: string;",
1037
965
  " entity_kind: K;",
@@ -1070,12 +998,6 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
1070
998
  " | {",
1071
999
  " readonly position: 'trackStart';",
1072
1000
  " };",
1073
- "export interface SetBgmInput {",
1074
- " readonly timelineEntityId: string;",
1075
- " readonly bgmClipEntityId: string;",
1076
- " readonly media: AudioMediaAssetFact;",
1077
- " readonly volume: number;",
1078
- "}",
1079
1001
  "export interface SetCaptionVisibilityInput {",
1080
1002
  " readonly timelineEntityId: string;",
1081
1003
  " readonly hidden: boolean;",
@@ -1134,68 +1056,6 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
1134
1056
  " entity_id: string;",
1135
1057
  " payload: JsonObject;",
1136
1058
  "}",
1137
- "export interface VideoMediaAssetFact {",
1138
- " readonly assetId: string;",
1139
- " readonly kind: 'video';",
1140
- " readonly durationMs: number;",
1141
- " readonly storageKey?: string;",
1142
- "}",
1143
- "export type VisualMediaAssetFact = ImageMediaAssetFact | VideoMediaAssetFact;",
1144
- "export interface VoiceDescriptor {",
1145
- " readonly system: 'voice-library';",
1146
- " readonly key: string;",
1147
- " readonly name?: string;",
1148
- "}",
1149
- "export interface VoiceMediaAssetFact {",
1150
- " /** Stable external speech result id, independent of the placed Clip id. */",
1151
- " readonly assetId: string;",
1152
- " readonly kind: 'voice';",
1153
- " readonly durationMs: number;",
1154
- " readonly storageKey: string;",
1155
- " /** Present for synthesized voice, absent for original recorded audio. */",
1156
- " readonly voice?: VoiceDescriptor;",
1157
- "}",
1158
- "export interface VoiceoverCaptionFact {",
1159
- " /** Stable placed caption identity supplied by the materialized side effect. */",
1160
- " readonly captionClipEntityId: string;",
1161
- " /** Directly held bases; includes the AudioScript used by the Voice. */",
1162
- " readonly baseEntityIds: readonly string[];",
1163
- " /** Ordered selection of AudioScript segments; caption text is never passed inline. */",
1164
- " readonly selections: readonly CaptionSegmentSelection[];",
1165
- " readonly startMs: number;",
1166
- " readonly durationMs: number;",
1167
- " readonly style?: CaptionStyleFields;",
1168
- "}",
1169
- "export type VoiceoverTakeInput = {",
1170
- " readonly timelineEntityId: string;",
1171
- " /** Stable placed speech identity, distinct from media.assetId. */",
1172
- " readonly voiceoverClipEntityId: string;",
1173
- " readonly media: VoiceMediaAssetFact;",
1174
- " /** Existing pronunciation variant; its composed AudioScript stays the text owner. */",
1175
- " readonly phoneticScriptEntityId: string;",
1176
- " readonly volume: number;",
1177
- " readonly captions: readonly VoiceoverCaptionFact[];",
1178
- "} & (",
1179
- " | {",
1180
- " readonly placement: ClipPlacement;",
1181
- " readonly hostClipEntityId?: never;",
1182
- " readonly anchorOffset?: never;",
1183
- " }",
1184
- " | {",
1185
- " readonly placement?: never;",
1186
- " readonly hostClipEntityId: string;",
1187
- " readonly anchorOffset: number;",
1188
- " }",
1189
- ");",
1190
- "export interface VoiceoverTakeResult {",
1191
- " readonly voiceoverClipEntityId: string;",
1192
- " readonly voiceEntityId: string;",
1193
- " /** The pronunciation variant the Voice was rendered from. */",
1194
- " readonly phoneticScriptEntityId: string;",
1195
- " /** The base-text owner resolved from the PhoneticScript baseEntityIds. */",
1196
- " readonly audioScriptEntityId: string;",
1197
- " readonly captionClipEntityIds: readonly string[];",
1198
- "}",
1199
1059
  "/** Timeline writes accept existing media Entity ids, never Memota asset ids or URLs. */",
1200
1060
  "export interface EditApi {",
1201
1061
  " insertClip(input: InsertClipInput): ClipEntityId;",
@@ -1205,21 +1065,15 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
1205
1065
  " moveSequentialClips(input: MoveSequentialClipsInput): void;",
1206
1066
  " moveClip(input: MoveClipInput): void;",
1207
1067
  " replaceClipContent(input: ReplaceClipContentInput): void;",
1208
- " insertMediaClip(input: InsertMediaClipInput): ClipEntityId;",
1209
- " insertMediaClips(input: InsertMediaClipsInput): readonly ClipEntityId[];",
1210
- " replaceMediaClip(input: ReplaceMediaClipInput): void;",
1211
1068
  " setClipVolume(input: SetClipVolumeInput): void;",
1212
1069
  " setClipSpeed(input: SetClipSpeedInput): void;",
1213
1070
  " trimClip(input: TrimClipInput): void;",
1214
- " replaceSequentialClips(input: ReplaceSequentialClipsInput): readonly ClipEntityId[];",
1215
1071
  " deleteClip(input: DeleteClipInput): void;",
1216
1072
  " deleteClipTree(input: DeleteClipTreeInput): void;",
1217
1073
  " updateClip(input: UpdateClipInput): void;",
1218
- " upsertVoiceoverTake(input: VoiceoverTakeInput): VoiceoverTakeResult;",
1219
1074
  " moveVoiceover(input: MoveVoiceoverInput): void;",
1220
1075
  " moveClipsToStarts(input: MoveClipsToStartsInput): void;",
1221
1076
  " deleteVoiceover(input: DeleteVoiceoverInput): void;",
1222
- " setBgm(input: SetBgmInput): ClipEntityId;",
1223
1077
  " deleteBgm(input: DeleteBgmInput): void;",
1224
1078
  " setCaptionVisibility(input: SetCaptionVisibilityInput): void;",
1225
1079
  " patchCaptionStyle(input: PatchCaptionStyleInput): void;",
@@ -1235,8 +1089,8 @@ const ENTITY_EDIT_SANDBOX_API_DTS = [
1235
1089
  "}",
1236
1090
  "export declare const edit: EditApi;",
1237
1091
  "export declare const timeline: TimelineApi;",
1238
- "export declare const entities: EntityFacade;",
1239
- "export declare const relations: RelationFacade;",
1092
+ "export declare const entities: BusinessEntityFacade;",
1093
+ "export declare const relations: BusinessRelationFacade;",
1240
1094
  "export declare function checkpoint(): SandboxCheckpoint;",
1241
1095
  "export declare function rollbackTo(cp: SandboxCheckpoint): void;",
1242
1096
  "export declare const inputs: Readonly<Record<string, unknown>>;",
@@ -1248,27 +1102,25 @@ const MEDEO_TOOL_DESCRIPTION = `
1248
1102
  Edit the authoritative Medeo Entity/Relation graph through a deterministic, side-effect-free JavaScript sandbox. Timeline objects and edit targets are Entities, not Memota assets or legacy parts.
1249
1103
 
1250
1104
  Operations:
1251
- - snapshot: initialize missing fixed editor structure, then return the Entity/Relation state summary and opaque base version. Initialization is idempotent and may advance the entity revision once; unchanged snapshots do not write.
1252
- - migrate-legacy: explicitly migrate an existing legacy timeline using recalled asset_facts. MEngine reads the canonical document and version, verifies that editing facts are preserved, and commits migration alone. Then take a fresh snapshot before any edit; never pass a caller-created legacy snapshot or version.
1253
- - run-edit-script: inspect timeline.snapshot(), entities.*, and relations.*; edit.* operates existing Entity ids and creates the required Clip/SequenceMarker structural graph. Asset import, media Entity creation and timeline edits belong in ONE plan. The sandbox has no network, storage or generation access. Pass recalled asset facts through inputs; generation history is not a script input — the host program queries it itself after each commit. A successful run returns preview, logs, base revision and plan_id.
1254
- - commit-plan: commit the complete Entity/Relation plan through revision CAS. The server derives the read-only timeline projection in the same transaction. There is no separate writable timeline plan and no preflight replay into a legacy editor. A failed transport is unconfirmed, never committed; retry the same plan_id.
1105
+ - snapshot: read the current Entity/Relation view, project attachments and causal Loro baseline. Reading does not initialize or mutate domain data.
1106
+ - run-edit-script: inspect timeline.snapshot(), entities.*, and relations.*; edit.* operates existing Entity ids and creates the required Clip/SequenceMarker structural graph. The sandbox has no network, storage or generation access. Use assembled entity fields; the host manages resource storage and generation provenance. A successful run returns preview, logs, base revision and plan_id.
1107
+ - commit-plan: publish the native Loro update compiled against the plan’s causal baseline. Concurrent independent edits merge through Loro. The timeline and AudioScript panel read the merged entity state. A failed transport is unconfirmed; retry the same plan_id so operation identities are preserved.
1255
1108
 
1256
- Default flow: snapshot → run-edit-script with auto_commit=false → inspect preview → commit-plan. Use auto_commit=true only for low-risk edits when the host does not need user confirmation. On version mismatch, rerun snapshot and the script; never try to patch a rejected journal by hand.
1109
+ Default flow: snapshot → run-edit-script with auto_commit=false → inspect preview → commit-plan. Use auto_commit=true only for low-risk edits when the host does not need user confirmation. Concurrent edits do not require replaying the script against a newer snapshot. If a domain conflict is reported, inspect the merged state and resolve it explicitly; never replace the complete document to force the edit through.
1257
1110
 
1258
- Generating an Asset alone does not require an Entity. Using that resource in the editor DOES: recall the Asset facts, call entities.ensureMedia(fact), then pass its contentEntityId to edit.insertClip. A raw external asset id or URL is not valid contentEntityId. Image/Video/Audio/Voice are logical variants of Asset: a resource has ONE identity and ONE typed row owning both media fields and external {system,key}/storageKey, with no separate Asset row or physical-asset relation. ensureMedia returns contentEntityId and reuses that single identity by external asset id. Each placement still gets its own Clip and SequenceMarker. Generation lineage is program-synced: after each successful commit the tool connects existing typed Assets from host-recalled generation facts (endpoint 0 output, endpoint 1 input) that the host queries itself — do not pass generation history through inputs. Do not author generated Relations yourself, and never create an Entity merely to backfill or represent lineage; media variants the edit itself legitimately needs are still created normally. Text-only generation has no input and no lineage edge. relations.of(entityId) is endpoint-agnostic.
1111
+ Generation tools return Asset references, not domain Entity ids. An Asset id may be passed as an entity resource field: external: { system: "memota", key: assetId }. Create or update the domain Entity using entities.*, then place its Entity id with edit.insertClip. Asset ids are not Entity ids, baseEntityIds or Relation endpoints. The sandbox exposes no Asset creation, lookup, reading or storage API; the host resolves resource references. Each placement has its own Clip and SequenceMarker. Generation lineage is host-synced; relations.of(entityId) is endpoint-agnostic.
1259
1112
  `.trim();
1260
1113
  const MEDEO_TOOL_EXECUTION_RULES = `
1261
1114
  The host supplies the current document. Do not ask for, invent, or pass a document id.
1262
1115
  timeline.snapshot() returns the Entity/Relation graph with its revision, not a legacy VideoDraft. Inspect Timeline, Track, Clip, SequenceMarker and their relations to plan edits.
1263
- Generation lineage is not a model input: the host program queries it via loadGenerationFacts and syncs generated Relations after each successful commit. Memota asset facts are host-provided through inputs. Never invent an asset id, Entity kind, or peer Entity id.
1264
- Use entities.ensureMedia(fact) to get or create the canonical typed Asset. Native media placement helpers use the same resolver. findByAssetId includes directly composed media variants, including Voice. Every Image/Video/Audio/Voice must own its external identity; separate Asset+media graphs are invalid. A typed Asset's external identity cannot be removed or rewritten: to replace its source, ensureMedia for the new Asset and replace the Clip's content. Conflicting facts fail closed. Asset generation itself still creates no editor Entities.
1265
- For recalled video/audio/voice, create a bounded/native payload whose extent end comes from factual media duration/coordinates in inputs; never fabricate a duration. Image uses unbounded/constant semantics and has no invented end. If required facts are absent, do not create the media Entity yet.
1266
- Caption content is assembled from AudioScript; never create an inline text Asset for it. Generated media lineage is host-owned; do not author generated Relations yourself. relations.of remains endpoint-agnostic for lookup.
1267
- The compatibility reader supports the existing four Track roles: video_clip (Image/Video), speech (Voice), caption (Caption), and bgm (Audio), one of each. Clip.volume is decibels (-60 to 20, 0 = original). Marker.sourceRange is the selected source interval; Marker.duration is effective display/playback duration. Coordinates and duration are whole milliseconds for this reader, not a global DSL restriction. Placement is exactly one of Clip.order, Marker.targetRange, or clip-anchor(child,host) plus Marker.anchorOffset. Use the native move/delete/voiceover helpers so placement and cascade decisions are in the same entity plan. Reading the graph never rebinds anchors or invents empty clips. Linear timeRemapping is {kind:'linear',rate:2,mode:'constant'} and agrees with rounded source span divided by rate. Image remains unbounded/constant; an explicit linear rate scales its display window, not an invented media extent. Nonlinear speed and multiple visual overlay tracks are unsupported.
1116
+ Inspect existing Image/Video/Audio/Voice Entities and their factual extents before placing them. For an uploaded or generated resource, create its media Entity with the returned Asset id in external.key and the factual media extent. The host resolves its physical resource. Replace a Clip's content using another content Entity id. Never fabricate a duration.
1117
+ Caption composes AudioScript text. Its optional external field may carry the Caption Asset id; the host resolves that resource. A Caption created from AudioScript may have no physical resource. Read and edit assembled fields through entities; do not create Asset entities or physical-asset Relations.
1118
+ The editor projection supports the existing four Track roles: video_clip (Image/Video), speech (Voice), caption (Caption), and bgm (Audio), one of each. Clip.volume is decibels (-60 to 20, 0 = original). Marker.sourceRange is the selected source interval; Marker.duration is effective display/playback duration. Coordinates and duration are whole milliseconds for this reader, not a global DSL restriction. Placement is exactly one of Clip.order, Marker.targetRange, or clip-anchor(child,host) plus Marker.anchorOffset. Use the native move/delete/voiceover helpers so placement and cascade decisions are in the same entity plan. Reading the graph never rebinds anchors or invents empty clips. Linear timeRemapping is {kind:'linear',rate:2,mode:'constant'} and agrees with rounded source span divided by rate. Image remains unbounded/constant; an explicit linear rate scales its display window, not an invented media extent. Nonlinear speed and multiple visual overlay tracks are unsupported.
1268
1119
  Entities own fields; ordinary Relations express associations; variants directly hold baseEntityIds and assemble the referenced entities. These foundations are fixed: implementation must follow them, never redefine them. Any entity may compose multiple bases. Equal field names from multiple bases (even equal values) are errors, even when the variant declares that field itself. After validating all base fields are unambiguous, explicitly declared own fields may override base fields without mutating the bases. Base ordering never resolves conflicts. AudioScript owns segmented text. Caption and PhoneticScript persist baseEntityIds including their AudioScript, plus their own fields; no composition Relation exists. Create the real bases before reading or committing a variant. Inside the DSL sandbox, entities.get/list expose complete assembled fields. Consumers read fields without inspecting base IDs or merging bases. entities.update patches supplied fields and routes inherited fields to their declaring entity; omitted fields remain unchanged. entities.declareFields explicitly declares own overrides and is distinct from an ordinary field edit. Persistence keeps owned fields only. entities.readCaptionContent(id) and entities.readPhoneticScriptContent(id) return assembled text. Missing/cyclic bases and field conflicts fail before persistence.
1269
- Use edit.insertCaptionClip with baseEntityIds and selections; each voiceover caption also supplies baseEntityIds. A selection names segmentId and may use a half-open Unicode code-point textRange to split a segment for the screen without rewriting AudioScript. Generate Voice from an existing PhoneticScript, then use phoneticScriptEntityId in the voiceover helper or relations.linkPhoneticScriptRender({output_entity_id,phonetic_script_entity_id}) for its render relation. Caption and Voice have their own Clips; display anchoring is explicit and independent of composition/alignment.
1120
+ Use edit.insertCaptionClip with baseEntityIds and selections, plus captionEntityId when generation returned a Caption identity; each voiceover caption also supplies baseEntityIds. A selection names segmentId and may use a half-open Unicode code-point textRange to split a segment for the screen without rewriting AudioScript. Generate Voice from an existing PhoneticScript, then use phoneticScriptEntityId in the voiceover helper or relations.linkPhoneticScriptRender({output_entity_id,phonetic_script_entity_id}) for its render relation. Caption and Voice have their own Clips; display anchoring is explicit and independent of composition/alignment.
1270
1121
  Move or stretch only the Clip's display Marker; preserve Caption intrinsic Sequence, AudioScript text and its annotation Markers. AudioScript cannot enter a Clip and has no intrinsic time. audio-script-source links its ASR source Audio/Video/Voice; audio-script-marker attaches annotation Markers with directly assigned segmentRanges:{segmentId,startMs,endMs} in whole milliseconds. Annotation Markers have no Clip/AXVideo/content/Timeline relations and never refer to other Markers for time. BGM keeps factual source duration with durationPolicy:'timeline'. Never introduce a speech entity kind.
1271
- Create only the known entity kinds. The host initializes one Timeline, four fixed Tracks, and the project's single AudioScript (initially segments:[]) before editing; inspect and reuse their IDs from timeline.snapshot(), never create another Timeline, Track, or AudioScript for each operation. The AudioScript identity is fixed for the project: edit its segments in place via entities.update, never delete or re-create it. If snapshot reports legacy migration is required, recall the listed asset facts and call migrate-legacy first. Missing facts, unsupported layouts, and version conflicts fail closed; never fall back to an old timeline method or raw update endpoint.
1122
+ Create only the known entity kinds. Project creation initializes one current Timeline, four Tracks and an attached AudioScript with segments:[]. Read the current AudioScript ID from timeline.snapshot(); the panel displays this attachment and preserves its segments. Normal edits operate this script, not an unrelated newly created script.
1123
+ Immutable updates apply to every entity: editing an owned field creates a new content version ID; changing only a variant's base ID preserves the variant ID. Editing a base through a variant updates the owner, and the compiler advances the affected base links and project attachment. A variant-owned edit creates a new variant version and retains its unchanged bases. Do not manually clone entities or duplicate inherited fields to implement versioning. Versions preserve native text and list editing identities so independent concurrent edits survive. Missing facts, unsupported layouts and composition conflicts are explicit errors; there is no legacy migration or whole-state overwrite path.
1272
1124
  Use only the globals and methods declared by the following TypeScript interface. Values not declared here are unavailable.
1273
1125
  `.trim();
1274
1126
  /** Render the complete MEngine-owned context injected before one model call. */
@@ -1294,42 +1146,6 @@ ${ENTITY_EDIT_SANDBOX_API_DTS}
1294
1146
  //#endregion
1295
1147
  //#region src/schema.ts
1296
1148
  const MEDEO_TOOL_NAME = "medeo";
1297
- const assetFactProperties = {
1298
- assetId: {
1299
- type: "string",
1300
- minLength: 1
1301
- },
1302
- kind: {
1303
- type: "string",
1304
- enum: [
1305
- "image",
1306
- "video",
1307
- "audio",
1308
- "voice"
1309
- ]
1310
- },
1311
- durationMs: {
1312
- type: "integer",
1313
- minimum: 1
1314
- },
1315
- storageKey: {
1316
- type: "string",
1317
- minLength: 1
1318
- },
1319
- voice: {
1320
- type: "object",
1321
- additionalProperties: false,
1322
- required: ["system", "key"],
1323
- properties: {
1324
- system: { const: "voice-library" },
1325
- key: {
1326
- type: "string",
1327
- minLength: 1
1328
- },
1329
- name: { type: "string" }
1330
- }
1331
- }
1332
- };
1333
1149
  /**
1334
1150
  * JSON Schema for the host-facing `medeo` tool surface.
1335
1151
  *
@@ -1347,7 +1163,6 @@ const MEDEO_TOOL_PARAMETERS = {
1347
1163
  type: "string",
1348
1164
  enum: [
1349
1165
  "snapshot",
1350
- "migrate-legacy",
1351
1166
  "run-edit-script",
1352
1167
  "commit-plan"
1353
1168
  ],
@@ -1367,68 +1182,6 @@ const MEDEO_TOOL_PARAMETERS = {
1367
1182
  type: "object",
1368
1183
  description: "Pre-materialized, side-effect-free values passed into the script, including recalled asset facts. Generation history is never an input: the host queries lineage itself and syncs generated Relations after each commit. Generation and network IO must happen in the host before this call."
1369
1184
  },
1370
- asset_facts: {
1371
- type: "array",
1372
- description: "Factual asset metadata recalled by the host for migrate-legacy only. The package reads the canonical legacy snapshot and version itself; never supply a clip trim window as media duration.",
1373
- items: { oneOf: [
1374
- {
1375
- type: "object",
1376
- additionalProperties: false,
1377
- required: ["assetId", "kind"],
1378
- properties: {
1379
- assetId: assetFactProperties.assetId,
1380
- kind: { const: "image" },
1381
- storageKey: assetFactProperties.storageKey
1382
- }
1383
- },
1384
- {
1385
- type: "object",
1386
- additionalProperties: false,
1387
- required: [
1388
- "assetId",
1389
- "kind",
1390
- "durationMs"
1391
- ],
1392
- properties: {
1393
- assetId: assetFactProperties.assetId,
1394
- kind: { const: "video" },
1395
- durationMs: assetFactProperties.durationMs,
1396
- storageKey: assetFactProperties.storageKey
1397
- }
1398
- },
1399
- {
1400
- type: "object",
1401
- additionalProperties: false,
1402
- required: [
1403
- "assetId",
1404
- "kind",
1405
- "durationMs",
1406
- "storageKey"
1407
- ],
1408
- properties: {
1409
- assetId: assetFactProperties.assetId,
1410
- kind: { const: "audio" },
1411
- durationMs: assetFactProperties.durationMs,
1412
- storageKey: assetFactProperties.storageKey
1413
- }
1414
- },
1415
- {
1416
- type: "object",
1417
- additionalProperties: false,
1418
- required: [
1419
- "assetId",
1420
- "kind",
1421
- "durationMs",
1422
- "storageKey",
1423
- "voice"
1424
- ],
1425
- properties: {
1426
- ...assetFactProperties,
1427
- kind: { const: "voice" }
1428
- }
1429
- }
1430
- ] }
1431
- },
1432
1185
  timeout_ms: {
1433
1186
  type: "integer",
1434
1187
  minimum: 1,
@@ -1450,24 +1203,11 @@ const MEDEO_TOOL_PARAMETERS = {
1450
1203
  },
1451
1204
  validation: {
1452
1205
  type: "string",
1453
- enum: ["version"],
1454
- description: "Commit with Entity revision CAS; reject concurrent changes."
1206
+ enum: ["preflight"],
1207
+ description: "Publish the native update already validated on its causal Loro fork."
1455
1208
  }
1456
1209
  },
1457
1210
  oneOf: [
1458
- {
1459
- required: [
1460
- "op",
1461
- "doc_id",
1462
- "asset_facts"
1463
- ],
1464
- properties: {
1465
- op: { const: "migrate-legacy" },
1466
- doc_id: { $ref: "#/properties/doc_id" },
1467
- asset_facts: { $ref: "#/properties/asset_facts" }
1468
- },
1469
- additionalProperties: false
1470
- },
1471
1211
  {
1472
1212
  required: ["op", "doc_id"],
1473
1213
  properties: {
@@ -1619,7 +1359,8 @@ function requiredContext(value, docId, field) {
1619
1359
  if (resolved == null || resolved.length === 0) throw new Error(`${field} must resolve to a non-empty string for doc ${docId}`);
1620
1360
  return resolved;
1621
1361
  }
1622
- function renderEntitySnapshot(state) {
1362
+ function renderEntitySnapshot(raw) {
1363
+ const state = businessState(raw);
1623
1364
  const rows = [...state.entities.map((entity) => JSON.stringify(entity)), ...state.relations.map((relation) => JSON.stringify(relation))];
1624
1365
  const shown = rows.slice(0, 200);
1625
1366
  return [
@@ -1628,61 +1369,25 @@ function renderEntitySnapshot(state) {
1628
1369
  ...shown.length < rows.length ? ["[truncated; inspect entities/relations in the sandbox]"] : []
1629
1370
  ].join("\n");
1630
1371
  }
1631
- function migrationNotice(document, state) {
1632
- if (state.entities.some((row) => row.entity_kind === "timeline") || Object.keys(document.part_library ?? {}).length === 0) return "";
1633
- const assetIds = new Set(Object.values(document.part_library ?? {}).flatMap((part) => {
1634
- const id = part.video_clip?.origin_media_id ?? part.bgm?.origin_media_id;
1635
- return typeof id === "string" && id !== "" ? [id] : [];
1636
- }));
1637
- return `\nLegacy timeline migration required. Recall factual media metadata for ${JSON.stringify([...assetIds])}, then call migrate-legacy with asset_facts. Speech facts are read from the canonical legacy document. Take a fresh snapshot after migration before editing.`;
1638
- }
1639
1372
  async function commitEntityPlan(client, plan) {
1640
- const rows = plan.entity_rows;
1641
- if (rows === void 0) throw new Error("entity plan is missing its authoritative rows");
1373
+ if (plan.entity_rows === void 0) throw new Error("entity plan is missing its authoritative rows");
1642
1374
  try {
1643
- const committed = await client.commit(plan.entity_base_revision, rows, {
1644
- deleted_entity_ids: plan.deleted_entity_ids ?? [],
1645
- deleted_relation_ids: plan.deleted_relation_ids ?? []
1646
- });
1375
+ if (!plan.loro_update) throw new Error("Entity plan has no compiled Loro update");
1376
+ const committed = await client.commitUpdate(plan.loro_update);
1647
1377
  return {
1648
1378
  kind: "committed",
1649
1379
  ops_applied: plan.entity_commands.length,
1650
- collaborated: false,
1380
+ collaborated: committed.revision > plan.entity_base_revision + 1,
1651
1381
  entity_revision: committed.revision
1652
1382
  };
1653
1383
  } catch (error) {
1654
1384
  if (error instanceof MengineEntityHttpRequestError) {
1655
- if (error.status === 409 && isRevisionConflictPayload(error.payload)) {
1656
- const actualFromPayload = revisionConflictActual(error.payload);
1657
- try {
1658
- const current = await client.fetchState();
1659
- if (current.revision === plan.entity_base_revision + 1 && entityRowsEquivalent(current, rows)) return {
1660
- kind: "committed",
1661
- ops_applied: plan.entity_commands.length,
1662
- collaborated: false,
1663
- entity_revision: current.revision
1664
- };
1665
- return {
1666
- kind: "rejected",
1667
- reason: "entity_revision_mismatch",
1668
- expected: plan.entity_base_revision,
1669
- actual: current.revision
1670
- };
1671
- } catch {
1672
- if (actualFromPayload !== void 0) return {
1673
- kind: "rejected",
1674
- reason: "entity_revision_mismatch",
1675
- expected: plan.entity_base_revision,
1676
- actual: actualFromPayload
1677
- };
1678
- return {
1679
- kind: "unconfirmed",
1680
- reason: "push_failed",
1681
- ops_applied: plan.entity_commands.length,
1682
- message: "entity-state conflict could not be reconciled"
1683
- };
1684
- }
1685
- }
1385
+ if (isRecord(error.payload) && error.payload.accepted === true && typeof error.payload.revision === "number") return {
1386
+ kind: "conflicted",
1387
+ accepted: true,
1388
+ entity_revision: error.payload.revision,
1389
+ message: entityHttpErrorMessage(error.payload)
1390
+ };
1686
1391
  return {
1687
1392
  kind: "rejected",
1688
1393
  reason: "entity_state_rejected",
@@ -1698,34 +1403,14 @@ async function commitEntityPlan(client, plan) {
1698
1403
  };
1699
1404
  }
1700
1405
  }
1701
- function revisionConflictActual(payload) {
1702
- if (!isRecord(payload)) return void 0;
1703
- const actual = payload.actual_revision;
1704
- return typeof actual === "number" && Number.isSafeInteger(actual) && actual >= 0 ? actual : void 0;
1705
- }
1706
- function isRevisionConflictPayload(payload) {
1707
- return isRecord(payload) && payload.code === "revision_conflict";
1708
- }
1709
1406
  function entityHttpErrorMessage(payload) {
1407
+ if (isRecord(payload) && isRecord(payload.error) && typeof payload.error.message === "string") return payload.error.message;
1710
1408
  if (isRecord(payload) && typeof payload.message === "string" && payload.message.length > 0) return payload.message;
1711
1409
  return typeof payload === "string" && payload.length > 0 ? payload : "mengine rejected the entity-state plan";
1712
1410
  }
1713
1411
  function commitWarnings(result) {
1714
1412
  return result.kind === "committed" && "warnings" in result && result.warnings !== void 0 ? [...result.warnings] : void 0;
1715
1413
  }
1716
- function entityRowsEquivalent(left, right) {
1717
- const normalize = (state) => ({
1718
- audioScriptEntityId: state.audioScriptEntityId,
1719
- entities: [...state.entities].sort((a, b) => a.entity_id.localeCompare(b.entity_id)).map((entity) => canonicalJson(entity)),
1720
- relations: [...state.relations].sort((a, b) => a.relation_id.localeCompare(b.relation_id)).map((relation) => canonicalJson(relation))
1721
- });
1722
- return JSON.stringify(normalize(left)) === JSON.stringify(normalize(right));
1723
- }
1724
- function canonicalJson(value) {
1725
- if (Array.isArray(value)) return value.map(canonicalJson);
1726
- if (!isRecord(value)) return value;
1727
- return Object.fromEntries(Object.keys(value).sort().map((key) => [key, canonicalJson(value[key])]));
1728
- }
1729
1414
  function parseInput(value) {
1730
1415
  if (!isRecord(value)) throw new Error("input must be an object");
1731
1416
  const op = value.op;
@@ -1736,18 +1421,6 @@ function parseInput(value) {
1736
1421
  op,
1737
1422
  doc_id: docId
1738
1423
  };
1739
- if (op === "migrate-legacy") {
1740
- if (Object.keys(value).some((key) => ![
1741
- "op",
1742
- "doc_id",
1743
- "asset_facts"
1744
- ].includes(key))) throw new Error("migrate-legacy accepts asset_facts only; the package reads the canonical document and version");
1745
- return {
1746
- op,
1747
- doc_id: docId,
1748
- asset_facts: parseMigrationAssetFacts(value.asset_facts)
1749
- };
1750
- }
1751
1424
  if (op === "run-edit-script") {
1752
1425
  if (typeof value.script !== "string" || value.script.length === 0) throw new Error("script must be a non-empty string");
1753
1426
  if (value.inputs !== void 0 && !isRecord(value.inputs)) throw new Error("inputs must be an object");
@@ -1768,7 +1441,7 @@ function parseInput(value) {
1768
1441
  }
1769
1442
  if (op === "commit-plan") {
1770
1443
  if (typeof value.plan_id !== "string" || value.plan_id.length === 0) throw new Error("plan_id must be a non-empty string");
1771
- if (value.validation !== void 0 && value.validation !== "version" && value.validation !== "preflight") throw new Error("validation must be \"version\" or \"preflight\"");
1444
+ if (value.validation !== void 0 && value.validation !== "preflight") throw new Error("validation must be \"preflight\"");
1772
1445
  return {
1773
1446
  op,
1774
1447
  doc_id: docId,
@@ -1857,10 +1530,21 @@ function createMedeoTool(options) {
1857
1530
  if (!(error instanceof MengineHttpRequestError) || error.status !== 404) throw error;
1858
1531
  if (options.loadInitialDraft === void 0) throw error;
1859
1532
  }
1860
- const seed = createMirrorVideoDocument(toVideoDocument(await options.loadInitialDraft(docId)), {
1533
+ const document = toVideoDocument(await options.loadInitialDraft(docId));
1534
+ if (Object.keys(document.part_library ?? {}).length > 0) throw new Error("Legacy content cannot bootstrap a Loro entity project");
1535
+ const seed = createMirrorVideoDocument(document, {
1861
1536
  ...peerId !== void 0 ? { peerId } : {},
1862
1537
  origin: "mengine.medeo_tool.bootstrap"
1863
1538
  });
1539
+ const foundation = ensureEditorFoundation({
1540
+ entities: [],
1541
+ relations: []
1542
+ });
1543
+ const entities = LoroEntityDocument.create(foundation.rows, {
1544
+ timelineEntityId: foundation.timelineEntityId,
1545
+ audioScriptEntityId: foundation.audioScriptEntityId
1546
+ });
1547
+ seed.import(entities.doc.export({ mode: "snapshot" }));
1864
1548
  try {
1865
1549
  await client.bootstrapSnapshot(seed.export({ mode: "snapshot" }));
1866
1550
  } catch (error) {
@@ -1909,64 +1593,31 @@ function createMedeoTool(options) {
1909
1593
  pendingPushes.delete(docId);
1910
1594
  if (plan.plan_kind === "timeline" && result.kind === "rejected" && result.reason === "push_rejected") documents.delete(docId);
1911
1595
  }
1912
- async function fetchEntityStateForSandbox(docId, doc, pull) {
1913
- const client = getEntityClient(docId);
1914
- for (let attempt = 0; attempt < 4; attempt += 1) {
1915
- const state = await client.fetchState();
1916
- if (pendingPushes.has(docId)) return state;
1917
- const document = doc.snapshot();
1918
- const hasTimeline = state.entities.some((row) => row.entity_kind === "timeline");
1919
- const hasLegacyContent = Object.keys(document.part_library ?? {}).length > 0 || (document.tracks ?? []).some((track) => (track.items ?? []).length > 0);
1920
- if (!hasTimeline && hasLegacyContent) return state;
1921
- if (!hasTimeline) {
1922
- if (pull.warnings !== void 0) throw new Error("Editor initialization requires a fresh canonical snapshot; retry snapshot");
1923
- const baseRows = toDslRows(state);
1924
- const migrated = migrateLegacyTimelineToEntities(document, [], baseRows);
1925
- try {
1926
- await getGraphClient(docId).commit({
1927
- revision: state.revision,
1928
- rows: baseRows
1929
- }, migrated, { migrationBaseVv: encodeDocVersionMark(doc.versionMark()) });
1930
- } catch (error) {
1931
- if (!(error instanceof MengineHttpRequestError) || error.status !== 409) throw new Error(`Editor initialization was not confirmed; retry snapshot to reconcile state: ${error instanceof Error ? error.message : String(error)}`);
1932
- }
1933
- pull = await observePull(doc);
1934
- continue;
1935
- }
1936
- const sandbox = new EntitySandbox({
1937
- state,
1938
- idFactory: (prefix) => `${prefix}_${randomUUID()}`
1939
- });
1940
- sandbox.ensureFoundation();
1941
- if (sandbox.commandCount === 0) return state;
1942
- if (pull.warnings !== void 0) throw new Error("Editor initialization requires a fresh canonical snapshot; retry snapshot");
1943
- try {
1944
- const committed = await client.commit(state.revision, sandbox.buildPlan().rows);
1945
- await doc.pull();
1946
- return committed;
1947
- } catch (error) {
1948
- if (!(error instanceof MengineEntityHttpRequestError) || error.status !== 409) throw new Error(`Editor initialization was not confirmed; retry snapshot to reconcile state: ${error instanceof Error ? error.message : String(error)}`);
1949
- pull = await observePull(doc);
1950
- }
1951
- }
1952
- throw new Error("Editor initialization conflicted repeatedly; take a fresh snapshot");
1953
- }
1954
- function getGraphClient(docId) {
1955
- return new EntityGraphHttpClient({
1956
- docId,
1957
- httpOrigin: requiredContext(options.httpOrigin, docId, "httpOrigin"),
1958
- ...options.authToken === void 0 ? {} : { authToken: () => optionalContext(options.authToken, docId) },
1959
- ...options.userId === void 0 ? {} : { userId: () => optionalContext(options.userId, docId) },
1960
- ...options.fetchImpl === void 0 ? {} : { fetchImpl: options.fetchImpl }
1961
- });
1596
+ async function fetchEntityStateForSandbox(docId, _doc, _pull) {
1597
+ const state = await getEntityClient(docId).fetchState();
1598
+ if (!state.loroSnapshot) throw new Error("Project requires the Loro entity contract");
1599
+ return state;
1962
1600
  }
1963
1601
  async function commitCachedPlan(docId, _doc, plan, validation, baseState) {
1964
1602
  if (plan.plan_kind === "timeline") throw new Error("Legacy timeline plans are not editable; use an Entity/Relation plan");
1965
- if (validation === "preflight") throw new Error("Entity plans use revision CAS; validation=preflight is not supported");
1966
1603
  if (plan.entity_rows === void 0) throw new Error("entity plan is missing its authoritative rows");
1967
1604
  const client = getEntityClient(docId);
1968
- const preCommitState = baseState ?? (options.loadGenerationFacts !== void 0 ? await client.fetchState() : void 0);
1969
- return await attachGenerationSync(docId, plan, await commitEntityPlan(client, plan), preCommitState);
1605
+ if (options.loadGenerationFacts !== void 0 && baseState === void 0) throw new Error("Generation synchronization is missing the cached causal plan baseline");
1606
+ const synced = await attachGenerationSync(docId, plan, await commitEntityPlan(client, plan), baseState);
1607
+ if (synced.kind !== "committed" || options.loadCaptionAssets === void 0) return synced;
1608
+ const assembly = await assembleCaptionAssets({
1609
+ client,
1610
+ docId,
1611
+ loadAssets: options.loadCaptionAssets
1612
+ });
1613
+ return {
1614
+ ...synced,
1615
+ asset_assembly: assembly,
1616
+ ...assembly.status === "failed" ? { warnings: [...synced.warnings ?? [], {
1617
+ kind: "asset_assembly_failed",
1618
+ message: assembly.message ?? "Caption Asset assembly failed"
1619
+ }] } : {}
1620
+ };
1970
1621
  }
1971
1622
  /**
1972
1623
  * After a confirmed entity commit, connect fact-matched generated Relations
@@ -2056,51 +1707,12 @@ function createMedeoTool(options) {
2056
1707
  op: "snapshot",
2057
1708
  doc_id: input.doc_id,
2058
1709
  version: `${encodeDocVersionMark(doc.versionMark())}:entities:${entityState.revision}`,
2059
- preview: renderEntitySnapshot(entityState) + migrationNotice(doc.snapshot(), entityState),
1710
+ preview: renderEntitySnapshot(entityState),
2060
1711
  collaborated: pull.collaborated,
2061
1712
  ...pull.warnings !== void 0 ? { warnings: pull.warnings } : {}
2062
1713
  };
2063
1714
  });
2064
1715
  }
2065
- async function migrate(input) {
2066
- return runExclusive(input.doc_id, async (doc) => {
2067
- assertNoPendingPush(input.doc_id);
2068
- const snapshotState = await getEntityClient(input.doc_id).fetchState();
2069
- if (snapshotState.entities.some((row) => row.entity_kind === "timeline")) return {
2070
- ok: true,
2071
- op: "migrate-legacy",
2072
- doc_id: input.doc_id,
2073
- migration_status: "already_entity",
2074
- entity_revision: snapshotState.revision,
2075
- next_action: "snapshot"
2076
- };
2077
- const pull = await doc.pull();
2078
- if (!pull.ok) throw new Error(`Migration requires a fresh canonical snapshot: ${pull.error.message}`);
2079
- const migrationBaseVv = encodeDocVersionMark(doc.versionMark());
2080
- const baseRows = toDslRows(snapshotState);
2081
- const nextRows = migrateLegacyTimelineToEntities(doc.snapshot(), input.asset_facts, baseRows);
2082
- let revision;
2083
- try {
2084
- revision = (await getGraphClient(input.doc_id).commit({
2085
- revision: snapshotState.revision,
2086
- rows: baseRows
2087
- }, nextRows, { migrationBaseVv })).revision;
2088
- } catch (error) {
2089
- if (error instanceof MengineHttpRequestError) throw new Error(`Migration rejected (HTTP ${error.status}): ${entityHttpErrorMessage(error.payload)}; take a fresh snapshot before retrying`);
2090
- throw new Error("Migration submission is unconfirmed; take a fresh snapshot and retry migrate-legacy to inspect whether the Entity timeline already exists");
2091
- }
2092
- documents.delete(input.doc_id);
2093
- for (const [id, cached] of plans) if (cached.docId === input.doc_id) plans.delete(id);
2094
- return {
2095
- ok: true,
2096
- op: "migrate-legacy",
2097
- doc_id: input.doc_id,
2098
- migration_status: "committed",
2099
- entity_revision: revision,
2100
- next_action: "snapshot"
2101
- };
2102
- });
2103
- }
2104
1716
  async function run(input) {
2105
1717
  return runExclusive(input.doc_id, async (doc) => {
2106
1718
  assertNoPendingPush(input.doc_id);
@@ -2210,7 +1822,6 @@ function createMedeoTool(options) {
2210
1822
  try {
2211
1823
  const parsed = parseInput(input);
2212
1824
  if (parsed.op === "snapshot") return await snapshot(parsed);
2213
- if (parsed.op === "migrate-legacy") return await migrate(parsed);
2214
1825
  if (parsed.op === "run-edit-script") return await run(parsed);
2215
1826
  return await commit(parsed);
2216
1827
  } catch (error) {
@@ -2243,6 +1854,139 @@ function createMedeoTool(options) {
2243
1854
  };
2244
1855
  }
2245
1856
  //#endregion
2246
- export { EditSandboxSession, MEDEO_TOOL_DESCRIPTION, MEDEO_TOOL_NAME, MEDEO_TOOL_PARAMETERS, collectAffectedPartIds, commitPlan, createMedeoTool, renderCompactProjection, renderPreview, runEditScript };
1857
+ //#region src/entity/materialize-resources.ts
1858
+ /** Persist resources before model consumption. The host serializes repeated imports of the same generation task. */
1859
+ async function materializeResources(options, resources) {
1860
+ const client = new EntityHttpClient(options);
1861
+ const state = await client.fetchState();
1862
+ let resourceKey = "";
1863
+ const sandbox = new EntitySandbox({
1864
+ state,
1865
+ idFactory: (prefix) => stableId(prefix, options.docId, resourceKey)
1866
+ });
1867
+ const ids = [];
1868
+ for (const resource of resources) {
1869
+ resourceKey = `${resource.kind}:${resource.assetId}`;
1870
+ if (resource.kind !== "caption") {
1871
+ ids.push(sandbox.entities.ensureMedia(resource).contentEntityId);
1872
+ continue;
1873
+ }
1874
+ const assetId = stableId("asset", resource.assetId);
1875
+ const asset = sandbox.entities.get(assetId);
1876
+ if (asset) {
1877
+ if (asset.entity_kind !== "asset" || asset.payload.storageKey !== resource.storageKey) throw new Error("Conflicting Caption resource identity");
1878
+ const captions = sandbox.relations.of(assetId, "physical-asset").map((edge) => sandbox.entities.get(edge.endpoint_0_entity_id)).filter((entity) => entity?.entity_kind === "caption");
1879
+ if (captions.length !== 1) throw new Error("Caption resource must resolve to one materialized Caption");
1880
+ ids.push(captions[0].entity_id);
1881
+ continue;
1882
+ }
1883
+ if (!resource.assetId.trim() || !resource.storageKey.trim() || !resource.segments.length) throw new Error("Caption resource requires a physical locator and timed segments");
1884
+ for (const segment of resource.segments) {
1885
+ if (typeof segment.text !== "string" || !Number.isFinite(segment.startMs) || !Number.isFinite(segment.endMs) || segment.startMs < 0 || segment.endMs <= segment.startMs) throw new Error("Caption resource has invalid ASR text or timing");
1886
+ for (const word of segment.words ?? []) if (typeof word.text !== "string" || !Number.isFinite(word.startMs) || !Number.isFinite(word.endMs) || word.startMs < segment.startMs || word.endMs > segment.endMs || word.endMs < word.startMs) throw new Error("Caption resource has invalid ASR word timing");
1887
+ }
1888
+ const scriptId = sandbox.audioScriptEntityId;
1889
+ if (!scriptId) throw new Error("Document AudioScript is not initialized");
1890
+ const script = sandbox.entities.get(scriptId);
1891
+ const segments = resource.segments.map((segment, index) => ({
1892
+ segmentId: stableId("segment", resource.assetId, String(index)),
1893
+ text: segment.text
1894
+ }));
1895
+ const start = Math.min(...resource.segments.map((segment) => segment.startMs));
1896
+ const end = Math.max(...resource.segments.map((segment) => segment.endMs));
1897
+ sandbox.entities.update({
1898
+ entity_id: scriptId,
1899
+ payload: { segments: [...script.payload.segments, ...segments] }
1900
+ });
1901
+ const captionId = stableId("entity", "caption", resource.assetId);
1902
+ sandbox.entities.create({
1903
+ entity_id: captionId,
1904
+ entity_kind: "caption",
1905
+ payload: {
1906
+ baseEntityIds: [scriptId],
1907
+ selections: segments.map(({ segmentId }) => ({ segmentId })),
1908
+ extent: {
1909
+ kind: "bounded",
1910
+ start,
1911
+ end
1912
+ },
1913
+ sampling: "native",
1914
+ coordinateSpace: "milliseconds"
1915
+ }
1916
+ });
1917
+ const markerId = sandbox.entities.create({
1918
+ entity_id: stableId("entity", "asr-marker", resource.assetId),
1919
+ entity_kind: "sequence-marker",
1920
+ payload: {
1921
+ sourceRange: {
1922
+ start,
1923
+ end
1924
+ },
1925
+ duration: { mode: "from-source" },
1926
+ segmentRanges: resource.segments.map((segment, index) => ({
1927
+ segmentId: segments[index].segmentId,
1928
+ startMs: segment.startMs,
1929
+ endMs: segment.endMs
1930
+ })),
1931
+ wordRanges: resource.segments.flatMap((segment, index) => (segment.words ?? []).map((word) => ({
1932
+ ...word,
1933
+ segmentIndex: index
1934
+ })))
1935
+ }
1936
+ });
1937
+ sandbox.relations.link({
1938
+ relation_id: stableId("relation", "asr-marker", resource.assetId),
1939
+ relation_kind: "audio-script-marker",
1940
+ endpoint_0_entity_id: scriptId,
1941
+ endpoint_1_entity_id: markerId
1942
+ });
1943
+ sandbox.entities.create({
1944
+ entity_id: assetId,
1945
+ entity_kind: "asset",
1946
+ payload: {
1947
+ external: {
1948
+ system: "memota",
1949
+ key: resource.assetId
1950
+ },
1951
+ storageKey: resource.storageKey
1952
+ }
1953
+ });
1954
+ sandbox.relations.link({
1955
+ relation_id: stableId("relation", captionId, assetId),
1956
+ relation_kind: "physical-asset",
1957
+ endpoint_0_entity_id: captionId,
1958
+ endpoint_1_entity_id: assetId
1959
+ });
1960
+ ids.push(captionId);
1961
+ }
1962
+ const candidate = sandbox.buildPlan();
1963
+ if (candidate.commands.length && options.loadGenerationFacts) {
1964
+ const newIds = new Set(ids.filter((id) => !state.entities.some((row) => row.entity_id === id)));
1965
+ const facts = parseGenerationFacts(await options.loadGenerationFacts(options.docId, resources.map((item) => item.assetId)));
1966
+ const relations = planGeneratedRelations({
1967
+ baseState: state,
1968
+ state: candidate.rows,
1969
+ scopedMediaIds: newIds,
1970
+ facts,
1971
+ newRelationId: () => `relation_${globalThis.crypto.randomUUID()}`
1972
+ });
1973
+ for (const relation of relations) sandbox.relations.linkGenerated({
1974
+ relation_id: relation.relation_id,
1975
+ output_entity_id: relation.endpoint_0_entity_id,
1976
+ input_entity_id: relation.endpoint_1_entity_id,
1977
+ trace: relation.trace
1978
+ });
1979
+ }
1980
+ const plan = sandbox.buildPlan();
1981
+ if (!plan.commands.length) return ids;
1982
+ const committed = await client.commit(state.revision, plan.rows);
1983
+ for (const id of ids) if (!committed.entities.some((entity) => entity.entity_id === id)) throw new Error(`Resource entity ${id} was not confirmed by the merged document`);
1984
+ return ids;
1985
+ }
1986
+ function stableId(prefix, ...parts) {
1987
+ return `${prefix}_${createHash("sha256").update(JSON.stringify(parts)).digest("hex")}`;
1988
+ }
1989
+ //#endregion
1990
+ export { EditSandboxSession, MEDEO_TOOL_DESCRIPTION, MEDEO_TOOL_NAME, MEDEO_TOOL_PARAMETERS, collectAffectedPartIds, commitPlan, createMedeoTool, materializeResources, renderCompactProjection, renderPreview, runEditScript };
2247
1991
 
2248
1992
  //# sourceMappingURL=index.mjs.map