Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript
35.9 KB
Raw Blame
import type {
  ColorLibraryBridge,
  TransitionLibraryBridge,
  EditingHost,
  JobKind,
  JobResult,
  JobRunner,
  TxnHandle,
  ProjectRef,
  ImportedMediaRef,
  RiggingBackendProbe,
  CreateProjectOptions,
  ImportMediaFromPathOptions,
  ImportMediaFromPathResult,
  ProjectFileRef,
  AppStateSnapshot,
  ProjectFontReport,
  ExportOptionsCatalog,
  LocalFileAccess,
  ModelInspectionReport,
  ModelInspectionRequest,
  HumanoidRigRequest,
  HumanoidRigResult,
  TextOverlayOptions,
  ShapeOverlayOptions,
  OverlayRef,
  OverlayKind,
  UpdateTextOverlayOptions,
  UpdateShapeOverlayOptions,
  StickerOverlayOptions,
  UpdateStickerOverlayOptions,
  SvgOverlayOptions,
  ExportMotionSceneOptions,
  ExportMotionSceneResult,
  ExportMotionSceneFormat,
  MotionRenderQueueBridge,
  RenderCacheBridge,
  MotionRenderQueueAddInput,
  MotionRenderQueueAddResult,
  MotionRenderQueueAddError,
  MotionRenderQueueRunResult,
  MulticamHostBridge,
  TimelinePlanBuilder,
} from "@openreel/agent";
import { planProjectFonts } from "../font-embedding";
import { createCustomFontReader } from "../../components/editor/inspector/font-options";
import type { TextStyle, TextAnimationPreset } from "@openreel/core/text/types";
import type { ShapeStyle, ShapeType } from "@openreel/core/graphics/types";
import type { Transform } from "@openreel/core/types/timeline";
import { CAPABILITY_MANIFEST } from "@openreel/core/capabilities/manifest";
import type { Action } from "@openreel/core/types/actions";
import type { Project } from "@openreel/core/types/project";
import type { CapabilityManifest } from "@openreel/core/capabilities/manifest";
import type { SequenceEditResult } from "@openreel/core/timeline/sequences";
import { commitSequenceEdit } from "../sequence-commands";
import { useGraphicTemplatesStore } from "../../stores/graphic-templates-store";
import type { GraphicTemplate } from "@openreel/core/graphic-templates/types";
import { useProjectStore } from "../../stores/project-store";
import { useUIStore } from "../../stores/ui-store";
import { useSourceMonitorStore } from "../../stores/source-monitor-store";
import { openInSourceMonitor, setSourceMarks } from "../source-monitor-commands";
import { analyzeAudioOffsets } from "../multicam-controller";
import { useEditorWorkspaceStore } from "../../stores/editor-workspace-store";
import { getLinkedTimelineSelection } from "../../utils/linked-timeline-selection";
import { insertTimelineOverlay } from "../../stores/project/insert-timeline-overlay";
import { checkForRecovery } from "../auto-save";
import {
  exportMotionCompositionScene,
  MOTION_EXPORT_FORMATS,
  type MotionExportFormat,
  type MotionExportRange,
  type MotionExportResolutionScale,
} from "../../motion/export-motion-frame";
import {
  useMotionStore,
  type MotionRenderQueueFormat,
} from "../../motion/stores/motion-store";
import { runMotionRenderQueue } from "../../motion/render-queue-runner";
import { renderCacheService } from "../render-cache-service";
import { createMulticamHostBridge } from "./multicam-bridge";
import { useColorLibraryStore } from "../../stores/color-library-store";
import { downloadCube } from "../color-library-service";
import { createColorMonitorBridge, createSceneDetectionBridge, createScopesBridge } from "./pro-tools-bridge";
import { createLoudnessBridge } from "./loudness-bridge";
import { createMotionTrackBridge } from "./motion-track-bridge";
import { createProxyBridge } from "./proxy-bridge";
import { createAudioRepairBridge } from "./audio-repair-bridge";
import { createSpectralSourceBridge } from "./spectral-source-bridge";
import { createStemExportBridge } from "./stem-bridge";
import { createExportQueueBridge } from "./export-queue-bridge";
import { createLocalFileAccess } from "./path-access";
import { importMediaFromPaths, openProjectFileByPath, saveProjectToFile } from "./path-import";
import { buildAppState, dismissOnboardingForAgent } from "./app-state";
import { loadExportCatalog } from "./export-options";
import { projectManager } from "../project-manager";

const MIME_BY_EXT: Record<string, string> = {
  mp4: "video/mp4",
  webm: "video/webm",
  mov: "video/quicktime",
  m4v: "video/mp4",
  mp3: "audio/mpeg",
  wav: "audio/wav",
  aac: "audio/aac",
  m4a: "audio/mp4",
  ogg: "audio/ogg",
  jpg: "image/jpeg",
  jpeg: "image/jpeg",
  png: "image/png",
  webp: "image/webp",
  gif: "image/gif",
};

function extFromUrl(url: string): string {
  try {
    const pathname = new URL(url).pathname;
    const dot = pathname.lastIndexOf(".");
    return dot >= 0 ? pathname.slice(dot + 1).toLowerCase() : "";
  } catch {
    return "";
  }
}

function inferName(url: string, mime: string): string {
  try {
    const base = new URL(url).pathname.split("/").filter(Boolean).pop();
    if (base && base.includes(".")) return decodeURIComponent(base);
  } catch {
    /* fall through */
  }
  const ext = (mime.split("/")[1] ?? "bin").replace("jpeg", "jpg");
  return `media-${Date.now()}.${ext}`;
}

function projectRef(project: Project): ProjectRef {
  return {
    id: project.id,
    name: project.name,
    width: project.settings.width,
    height: project.settings.height,
    frameRate: project.settings.frameRate,
    modifiedAt: project.modifiedAt,
  };
}

interface LiveEditorHostOptions {
  readonly jobRunner?: JobRunner;
}

const RENDER_QUEUE_FORMATS: readonly MotionRenderQueueFormat[] = [
  "mp4",
  "webm-alpha",
  "mov-prores4444",
  "png-sequence",
];

const RENDER_QUEUE_RESOLUTION_SCALES: readonly MotionExportResolutionScale[] = [
  1, 0.5, 0.25,
];

function normalizeRenderQueueFormat(
  format: string | undefined,
): MotionRenderQueueFormat | undefined {
  if (format === undefined) return "mp4";
  return (RENDER_QUEUE_FORMATS as readonly string[]).includes(format)
    ? (format as MotionRenderQueueFormat)
    : undefined;
}

function normalizeRenderQueueScale(
  scale: number | undefined,
): MotionExportResolutionScale | undefined | null {
  if (scale === undefined) return undefined;
  return (RENDER_QUEUE_RESOLUTION_SCALES as readonly number[]).includes(scale)
    ? (scale as MotionExportResolutionScale)
    : null;
}

/**
 * EditingHost backed by the live web editor's Zustand store. Used by the
 * built-in chat (in-renderer) and by the desktop MCP server (forwarded over
 * IPC). Edits go through the same undoable action path the UI uses, so the chat
 * and the timeline stay in sync and the whole turn undoes as one history group.
 */
export class LiveEditorHost implements EditingHost {
  private jobRunner?: JobRunner;
  private appliedInTxn = 0;
  readonly multicam: MulticamHostBridge = createMulticamHostBridge((timeMs) =>
    this.runJob("exportFrame", { time: timeMs / 1_000 }),
  );

  readonly sceneDetection = createSceneDetectionBridge();
  readonly scopes = createScopesBridge();
  readonly colorMonitor = createColorMonitorBridge();
  readonly loudness = createLoudnessBridge();
  readonly stems = createStemExportBridge();
  readonly audioRepair = createAudioRepairBridge();
  readonly spectralSource = createSpectralSourceBridge();
  readonly exportQueue = createExportQueueBridge();
  /** Allowed-roots-checked file access; present on the desktop app only. */
  readonly files: LocalFileAccess | undefined = createLocalFileAccess();
  readonly proxies = createProxyBridge();
  readonly motionTracking = createMotionTrackBridge();

  constructor(options: LiveEditorHostOptions = {}) {
    this.jobRunner = options.jobRunner;
  }

  setJobRunner(runner: JobRunner): void {
    this.jobRunner = runner;
  }

  getProject(): Project {
    this.requireOpenProject();
    return useProjectStore.getState().project;
  }

  async applyAction(action: Action) {
    this.requireOpenProject();
    const result = await useProjectStore.getState().executeAction(action);
    if (result.success) this.appliedInTxn++;
    return result;
  }

  /**
   * Plans against the full project (overlay collections included) and commits
   * through commitTimelineToolEdit, which also resyncs the effects, overlay and
   * transition caches. Exactly one history entry.
   */
  async commitTimelinePlan(build: TimelinePlanBuilder) {
    this.requireOpenProject();
    const store = useProjectStore.getState();
    try {
      const plan = build(store.getFullProject());
      if (!plan.edits.length) return { success: true, plan };
      const result = store.commitTimelineToolEdit(plan);
      if (result.success) this.appliedInTxn++;
      return { ...result, plan };
    } catch (error) {
      return {
        success: false,
        error: {
          code: "INVALID_PARAMS" as const,
          message: error instanceof Error ? error.message : "The edit could not be planned.",
        },
      };
    }
  }

  /** Same linked-selection logic (and setting) the timeline UI uses. */
  resolveLinkedClipIds(project: Project, clipIds: readonly string[]): string[] {
    return getLinkedTimelineSelection(project, clipIds, useEditorWorkspaceStore.getState().linkedSelection);
  }

  sourceMonitor = {
    open: (mediaId: string, time?: number): void => {
      openInSourceMonitor(mediaId, time);
    },
    getState: () => {
      const { mediaId, position } = useSourceMonitorStore.getState();
      return { mediaId, position };
    },
    getMarks: (mediaId: string) => ({ ...useSourceMonitorStore.getState().getMarks(mediaId) }),
    setMarks: (mediaId: string, marks: { in?: number | null; out?: number | null }): void => {
      setSourceMarks(mediaId, marks);
    },
  };

  targeting = {
    getState: () => {
      const { targetedTrackIds, targetTracks, channelPatches, disabledChannels } = useEditorWorkspaceStore.getState();
      return {
        targetedTrackIds: [...targetedTrackIds],
        sourcePatch: { ...targetTracks },
        channelPatches: { ...channelPatches },
        disabledChannels: [...disabledChannels],
      };
    },
    setChannelPatch: (channel: number, trackId: string | null): void => {
      useEditorWorkspaceStore.getState().setChannelPatch(channel, trackId);
    },
    setChannelEnabled: (channel: number, enabled: boolean): void => {
      useEditorWorkspaceStore.getState().setChannelEnabled(channel, enabled);
    },
    setTrackTargeted: (trackId: string, targeted: boolean): void => {
      useEditorWorkspaceStore.getState().setTrackTargeted(trackId, targeted);
    },
    setSourcePatch: (kind: "video" | "audio", trackId: string | null): void => {
      useEditorWorkspaceStore.getState().setSourcePatch(kind, trackId);
    },
  };

  /** Same command path as the sequence tabs: parks In/Out and the playhead when the active sequence changes. */
  sequenceCommands = {
    commit: (label: string, build: (project: Project) => SequenceEditResult): Promise<SequenceEditResult> =>
      commitSequenceEdit(label, build),
  };

  /** Same audio correlation the Create multi-camera source dialog runs. */
  multicamSync = {
    measureAudioOffsets: (mediaIds: readonly string[]) => analyzeAudioOffsets(mediaIds),
  };

  /** Essential Graphics: saved templates live in the same library the Browse tab shows. */
  graphicTemplates = {
    list: () => useGraphicTemplatesStore.getState().userTemplates,
    save: (template: GraphicTemplate): void => useGraphicTemplatesStore.getState().save(template),
  };

  /** Per-user LUT library and saved looks (IndexedDB): the same library the Color tab shows. */
  readonly colorLibrary: ColorLibraryBridge = {
    listLooks: async () => {
      await useColorLibraryStore.getState().refresh();
      return useColorLibraryStore.getState().looks;
    },
    saveLook: (look) => useColorLibraryStore.getState().addLook(look),
    deleteLook: (id) => useColorLibraryStore.getState().removeLook(id),
    listLuts: async () => {
      await useColorLibraryStore.getState().refresh();
      return useColorLibraryStore.getState().luts;
    },
    saveLut: (entry) => useColorLibraryStore.getState().addLut(entry),
    deleteLut: (id) => useColorLibraryStore.getState().removeLut(id),
    exportCube: async (filename, text) => downloadCube(filename, text),
  };

  /** App-wide transition presets (IndexedDB): the same library the Transitions panel shows. Loaded on first use. */
  readonly transitionLibrary: TransitionLibraryBridge = {
    list: async () => (await import("../global-transition-presets")).getGlobalTransitionPresets().list(),
    save: async (name, source) => {
      const result = await (await import("../global-transition-presets")).getGlobalTransitionPresets().save(name, source);
      return result.ok ? { ok: true, id: result.value.id } : { ok: false, error: result.error };
    },
    remove: async (id) => {
      const result = await (await import("../global-transition-presets")).getGlobalTransitionPresets().remove(id);
      return result.ok ? { ok: true } : { ok: false, error: result.error };
    },
    exportJson: async () => (await import("../global-transition-presets")).getGlobalTransitionPresets().exportJson(),
    importJson: async (text) => {
      const result = await (await import("../global-transition-presets")).getGlobalTransitionPresets().importJson(text);
      return result.ok ? { ok: true, added: result.value.added.length, skipped: result.value.skipped.length } : { ok: false, error: result.error };
    },
  };

  /** Match Frame: select the source media in the library like the UI does. */
  selectLibraryMedia(mediaId: string): void {
    useUIStore.getState().select({ type: "clip", id: mediaId });
    useEditorWorkspaceStore.getState().setSection("media");
  }

  beginTransaction(label?: string): TxnHandle {
    this.appliedInTxn = 0;
    useProjectStore.getState().beginHistoryGroup(label);
    return { id: label ?? "turn" };
  }

  commitTransaction(_handle: TxnHandle, _label: string): void {
    useProjectStore.getState().endHistoryGroup();
  }

  async rollbackTransaction(_handle: TxnHandle): Promise<void> {
    useProjectStore.getState().endHistoryGroup();
    // The turn's actions form one history group; a single undo reverts them all.
    if (this.appliedInTxn > 0) {
      await useProjectStore.getState().undo();
    }
    this.appliedInTxn = 0;
  }

  async runJob(
    kind: JobKind,
    params: Record<string, unknown>,
  ): Promise<JobResult> {
    if (!this.jobRunner) {
      return { ok: false, error: `Job '${kind}' is not wired in the live host yet` };
    }
    return this.jobRunner(kind, params);
  }

  capabilities(): CapabilityManifest {
    return CAPABILITY_MANIFEST;
  }

  requireOpenProject(): void {
    if (!useProjectStore.getState().hasOpenProject) {
      throw new Error("No project is open");
    }
  }

  async createProject(options: CreateProjectOptions): Promise<ProjectRef> {
    const settings: Partial<Project["settings"]> = {
      ...(options.width !== undefined ? { width: options.width } : {}),
      ...(options.height !== undefined ? { height: options.height } : {}),
      ...(options.frameRate !== undefined ? { frameRate: options.frameRate } : {}),
    };
    useProjectStore.getState().createNewProject(options.name, settings);
    return projectRef(useProjectStore.getState().project);
  }

  async listProjects(): Promise<readonly ProjectRef[]> {
    const saves = await checkForRecovery();
    const latest = new Map<string, ProjectRef>();
    for (const s of saves) {
      const existing = latest.get(s.projectId);
      if (!existing || (existing.modifiedAt ?? 0) < s.timestamp) {
        latest.set(s.projectId, { id: s.id, name: s.projectName, modifiedAt: s.timestamp });
      }
    }
    // Recent project files on disk (desktop): reopen them with open_project_file.
    const recents = await projectManager.getRecentProjects().catch(() => []);
    for (const recent of recents) {
      const handle = recent.fileHandle as { kind?: string; path?: string } | undefined;
      if (handle?.kind === "native" && typeof handle.path === "string") {
        latest.set(`file:${handle.path}`, { id: recent.id, name: recent.name, modifiedAt: recent.lastOpened, path: handle.path });
      }
    }
    return [...latest.values()].sort((a, b) => (b.modifiedAt ?? 0) - (a.modifiedAt ?? 0));
  }

  async openProject(id: string): Promise<ProjectRef> {
    const ok = await useProjectStore.getState().recoverFromAutoSave(id);
    if (!ok) throw new Error(`Could not open project (save id "${id}")`);
    return projectRef(useProjectStore.getState().project);
  }

  async saveProject(): Promise<ProjectRef> {
    this.requireOpenProject();
    const store = useProjectStore.getState();
    await store.forceSave();
    return projectRef(store.getFullProject());
  }

  importMediaFromPath(options: ImportMediaFromPathOptions): Promise<ImportMediaFromPathResult> {
    this.requireOpenProject();
    return importMediaFromPaths(this, options);
  }

  openProjectFile(path: string): Promise<ProjectFileRef> {
    return openProjectFileByPath(path);
  }

  saveProjectAs(path: string, options: { overwrite: boolean }): Promise<ProjectFileRef> {
    return saveProjectToFile(path, options);
  }

  async getProjectFonts(): Promise<ProjectFontReport> {
    const reader = await createCustomFontReader();
    const plan = await planProjectFonts(useProjectStore.getState().getFullProject(), reader.read, { isUserFont: reader.isUserFont });
    return { fonts: plan.entries };
  }

  getAppState(): Promise<AppStateSnapshot> {
    return buildAppState();
  }

  dismissOnboarding(): Promise<{ status: string; changed: boolean }> {
    return dismissOnboardingForAgent();
  }

  exportOptions(): Promise<ExportOptionsCatalog> {
    return loadExportCatalog(useProjectStore.getState().project);
  }

  async importMediaFromUrl(
    url: string,
    options?: { name?: string },
  ): Promise<ImportedMediaRef> {
    this.requireOpenProject();
    const bridge = window.openreel?.media?.fetchUrl;
    if (typeof bridge !== "function") {
      throw new Error("Media download is only available in the desktop app");
    }
    const res = await bridge({ url });
    if (!res.ok) {
      throw new Error(res.error ?? `Download failed: HTTP ${res.status}`);
    }
    const mime = (res.contentType.split(";")[0] ?? "").trim() || MIME_BY_EXT[extFromUrl(url)] || "application/octet-stream";
    const name = options?.name ?? inferName(url, mime);
    const file = new File([res.body], name, { type: mime });
    const result = await useProjectStore.getState().importMedia(file);
    if (!result.success || !result.actionId) {
      throw new Error(result.error?.message ?? "Media import failed");
    }
    const mediaId = result.actionId;
    const item = useProjectStore
      .getState()
      .project.mediaLibrary.items.find((m) => m.id === mediaId);
    return {
      mediaId,
      name,
      type: item?.type ?? "unknown",
      durationSec: item?.metadata?.duration ?? 0,
      width: item?.metadata?.width,
      height: item?.metadata?.height,
    };
  }

  async exportMotionScene(
    options: ExportMotionSceneOptions,
  ): Promise<ExportMotionSceneResult> {
    this.requireOpenProject();
    const project = useProjectStore.getState().project;
    const compositions = project.motionCompositions ?? [];
    const composition = compositions.find(
      (candidate) => candidate.id === options.compositionId,
    );
    if (!composition) {
      throw new Error(`Motion composition not found: ${options.compositionId}`);
    }
    const requestedFormat: ExportMotionSceneFormat = options.format ?? "mp4";
    const descriptor = MOTION_EXPORT_FORMATS.find(
      (entry) => entry.id === requestedFormat,
    );
    const requiresNative =
      descriptor !== undefined &&
      (descriptor.transparent || descriptor.extension === "mov");
    const nativeAvailable =
      typeof window !== "undefined" &&
      window.openreel?.platform === "desktop";
    const willNormalize = requiresNative && !nativeAvailable;
    if (willNormalize && options.acknowledgeH264Fallback !== true) {
      throw new Error(
        "Web export can't produce ProRes or alpha — this will encode H.264 without transparency. Pass acknowledgeH264Fallback:true to consent, or use the desktop app for ProRes/alpha.",
      );
    }
    const encodedFormat: ExportMotionSceneFormat = willNormalize
      ? "mp4"
      : requestedFormat;
    const result = await exportMotionCompositionScene({
      project,
      composition,
      compositionLibrary: compositions.length > 0 ? compositions : [composition],
      format: encodedFormat as MotionExportFormat,
      ...(options.filename ? { filename: options.filename } : {}),
    });
    return {
      filename: result.filename,
      width: result.width,
      height: result.height,
      duration: result.duration,
      framesRendered: result.framesRendered,
      requestedFormat,
      encodedFormat,
      normalizedToH264: willNormalize,
      ...(willNormalize
        ? { note: "Encoded H.264 (ProRes/alpha unavailable on web)" }
        : {}),
    };
  }

  renderCache: RenderCacheBridge = {
    renderInToOut: async (options) => {
      const override =
        options?.rangeStart !== undefined && options?.rangeEnd !== undefined
          ? { start: options.rangeStart, end: options.rangeEnd }
          : undefined;
      const summary = options?.effectsOnly
        ? await renderCacheService.renderEffectsInToOut(override)
        : await renderCacheService.renderInToOut(override);
      if (!summary) {
        return {
          status: "failed",
          framesRendered: 0,
          framesSkipped: 0,
          totalFrames: 0,
          elapsedMs: 0,
          codec: "",
          message: "A render is already running, an export is in progress, or there is nothing to render.",
        };
      }
      return {
        status: summary.status,
        framesRendered: summary.framesRendered,
        framesSkipped: summary.framesSkipped,
        totalFrames: summary.totalFrames,
        elapsedMs: summary.elapsedMs,
        codec: summary.codec,
        message: summary.message,
      };
    },
    deleteRenderFiles: async () => ({
      removedFrames: await renderCacheService.deleteRenderFiles({ silent: true }),
    }),
    getStatus: () => renderCacheService.getStatus(),
  };

  motionRenderQueue: MotionRenderQueueBridge = {
    add: (
      input: MotionRenderQueueAddInput,
    ): MotionRenderQueueAddResult | MotionRenderQueueAddError => {
      if (!useProjectStore.getState().hasOpenProject) {
        return { error: "No project is open" };
      }
      const format = normalizeRenderQueueFormat(input.format);
      if (format === undefined) {
        return { error: `Unsupported render queue format: ${input.format}` };
      }
      const scale = normalizeRenderQueueScale(input.resolutionScale);
      if (scale === null) {
        return {
          error: `resolutionScale must be one of ${RENDER_QUEUE_RESOLUTION_SCALES.join(", ")}`,
        };
      }
      const project = useProjectStore.getState().project;
      const compositions = project.motionCompositions ?? [];
      const scene = compositions.find(
        (candidate) => candidate.id === input.compositionId,
      );
      if (!scene) {
        return {
          error: `Motion composition not found: ${input.compositionId}`,
        };
      }
      let range: MotionExportRange | undefined;
      if (input.range !== undefined) {
        const { startTime, endTime } = input.range;
        if (
          !Number.isFinite(startTime) ||
          !Number.isFinite(endTime) ||
          !(startTime < endTime) ||
          startTime < 0 ||
          endTime > scene.duration
        ) {
          return {
            error: "range must satisfy 0 <= startTime < endTime <= duration",
          };
        }
        range = { startTime, endTime };
      }
      const itemId = useMotionStore.getState().addRenderQueueItem({
        compositionId: scene.id,
        name: scene.name,
        width: scene.width,
        height: scene.height,
        frameRate: scene.frameRate,
        duration: scene.duration,
        format,
        ...(range !== undefined ? { range } : {}),
        ...(scale !== undefined ? { resolutionScale: scale } : {}),
      });
      return { itemId };
    },
    run: async (): Promise<MotionRenderQueueRunResult> => {
      if (!useProjectStore.getState().hasOpenProject) {
        return { outcomes: [], alreadyRunning: false };
      }
      const project = useProjectStore.getState().project;
      const compositions = project.motionCompositions ?? [];
      const result = await runMotionRenderQueue({ project, compositions });
      if (result.alreadyRunning) {
        return { outcomes: [], alreadyRunning: true };
      }
      return {
        alreadyRunning: false,
        outcomes: result.outcomes.map((outcome) => ({
          itemId: outcome.itemId,
          status: outcome.status,
          ...(outcome.encodedFormat !== undefined
            ? { encodedFormat: outcome.encodedFormat }
            : {}),
          ...(outcome.filename !== undefined
            ? { filename: outcome.filename }
            : {}),
          ...(outcome.error !== undefined ? { error: outcome.error } : {}),
        })),
      };
    },
    list: (): ReadonlyArray<Record<string, unknown>> =>
      useMotionStore.getState().renderQueue.map((item) => ({
        itemId: item.id,
        compositionId: item.compositionId,
        name: item.name,
        format: item.format,
        status: item.status,
        progress: item.progress,
        ...(item.range !== undefined ? { range: item.range } : {}),
        ...(item.resolutionScale !== undefined
          ? { resolutionScale: item.resolutionScale }
          : {}),
        ...(item.outputFilename !== undefined
          ? { filename: item.outputFilename }
          : {}),
        ...(item.error !== undefined ? { error: item.error } : {}),
      })),
    cancel: (itemId: string): boolean => {
      const exists = useMotionStore
        .getState()
        .renderQueue.some((item) => item.id === itemId);
      if (!exists) return false;
      useMotionStore.getState().cancelRenderQueueItem(itemId);
      return true;
    },
  };

  async probeRiggingBackend(): Promise<RiggingBackendProbe> {
    const probeBackend = window.openreel?.rigging?.probeBackend;
    if (typeof probeBackend !== "function") {
      return {
        available: false,
        provider: "blender",
        error: "Rigging backend is only available in the desktop app",
      };
    }
    return probeBackend();
  }

  async inspectModel(options: ModelInspectionRequest): Promise<ModelInspectionReport> {
    const { inspectGltfModel } = await import("../../motion/model-inspection");
    return inspectGltfModel(options.modelUrl, {
      ...(options.name ? { name: options.name } : {}),
      ...(options.source ? { source: options.source } : {}),
    });
  }

  async rigHumanoidModel(options: HumanoidRigRequest): Promise<HumanoidRigResult> {
    const rigHumanoidModel = window.openreel?.rigging?.rigHumanoidModel;
    if (typeof rigHumanoidModel !== "function") {
      return {
        ok: false,
        provider: "blender",
        inputUrl: options.modelUrl,
        createdArmature: false,
        preservedExistingArmature: false,
        skinnedMeshCount: 0,
        meshCount: 0,
        boneCount: 0,
        warnings: [
          {
            code: "HOST_UNAVAILABLE",
            severity: "error",
            message: "Humanoid rigging is only available in the desktop app.",
          },
        ],
        error: "Humanoid rigging is only available in the desktop app",
      };
    }
    return rigHumanoidModel({
      modelUrl: options.modelUrl,
      ...(options.outputPath ? { outputPath: options.outputPath } : {}),
      ...(options.name ? { name: options.name } : {}),
      ...(options.heightMeters ? { heightMeters: options.heightMeters } : {}),
      ...(options.overwriteExisting !== undefined
        ? { overwriteExisting: options.overwriteExisting }
        : {}),
    });
  }

  async createTextOverlay(options: TextOverlayOptions): Promise<OverlayRef> {
    this.requireOpenProject();
    const clip = await insertTimelineOverlay(
      options.startSec,
      options.durationSec,
      (trackId) =>
        useProjectStore
          .getState()
          .createTextClip(
            trackId,
            options.startSec,
            options.text,
            options.durationSec,
            options.style as Partial<TextStyle> | undefined,
          ),
      options.trackId,
    );
    if (!clip) throw new Error("Failed to create text overlay");

    if (options.animation && options.animation !== "none") {
      useProjectStore
        .getState()
        .applyTextAnimationPreset(
          clip.id,
          options.animation as TextAnimationPreset,
          options.animationInSec ?? 0.3,
          options.animationOutSec ?? 0.25,
        );
    }
    return { id: clip.id, trackId: clip.trackId };
  }

  async createShapeOverlay(options: ShapeOverlayOptions): Promise<OverlayRef> {
    this.requireOpenProject();
    const style: Partial<ShapeStyle> = {
      fill: { type: "solid", color: options.color ?? "#000000", opacity: options.opacity ?? 0.45 },
      stroke: { color: "#000000", width: 0, opacity: 0 },
    };
    const clip = await insertTimelineOverlay(
      options.startSec,
      options.durationSec,
      (trackId) =>
        useProjectStore
          .getState()
          .createShapeClip(
            trackId,
            options.startSec,
            (options.shapeType ?? "rectangle") as ShapeType,
            options.durationSec,
            style,
            {
              ...(options.points ? { points: options.points.map((p) => ({ x: p.x, y: p.y })) } : {}),
              ...(options.pathData !== undefined ? { pathData: options.pathData } : {}),
              ...(options.closed !== undefined ? { closed: options.closed } : {}),
            },
          ),
      options.trackId,
    );
    if (!clip) throw new Error("Failed to create shape overlay");

    if (options.fullFrame) {
      const { width, height } = useProjectStore.getState().project.settings;
      useProjectStore.getState().updateShapeTransform(clip.id, {
        position: { x: 0.5, y: 0.5 },
        scale: { x: width / 200, y: height / 200 },
        anchor: { x: 0.5, y: 0.5 },
        rotation: 0,
        opacity: 1,
      });
    }
    return { id: clip.id, trackId: clip.trackId };
  }

  async updateTextOverlay(
    id: string,
    options: UpdateTextOverlayOptions,
  ): Promise<OverlayRef> {
    this.requireOpenProject();
    const store = useProjectStore.getState();
    if (!store.getTextClip(id)) {
      throw new Error(`Text overlay "${id}" not found`);
    }
    if (options.text !== undefined && !store.updateTextContent(id, options.text)) {
      throw new Error(`Failed to update text overlay "${id}" content`);
    }
    if (options.style !== undefined) {
      if (!store.updateTextStyle(id, options.style as Partial<TextStyle>)) {
        throw new Error(`Failed to update text overlay "${id}" style`);
      }
    }
    if (options.transform !== undefined) {
      if (!store.updateTextTransform(id, options.transform as Partial<Transform>)) {
        throw new Error(`Failed to update text overlay "${id}" transform`);
      }
    }
    if (options.animation && options.animation !== "none") {
      const updated = store.applyTextAnimationPreset(
        id,
        options.animation as TextAnimationPreset,
        options.animationInSec ?? 0.3,
        options.animationOutSec ?? 0.25,
      );
      if (!updated) throw new Error(`Failed to update text overlay "${id}" animation`);
    }
    const clip = useProjectStore.getState().getTextClip(id);
    if (!clip) throw new Error(`Text overlay "${id}" not found`);
    return { id, trackId: clip.trackId };
  }

  async updateShapeOverlay(
    id: string,
    options: UpdateShapeOverlayOptions,
  ): Promise<OverlayRef> {
    this.requireOpenProject();
    const store = useProjectStore.getState();
    if (!store.getShapeClip(id)) {
      throw new Error(`Shape overlay "${id}" not found`);
    }
    if (
      options.style !== undefined ||
      options.color !== undefined ||
      options.opacity !== undefined
    ) {
      const style: Partial<ShapeStyle> =
        (options.style as Partial<ShapeStyle> | undefined) ?? {
          fill: {
            type: "solid",
            color: options.color ?? "#000000",
            opacity: options.opacity ?? 1,
          },
        };
      if (!store.updateShapeStyle(id, style)) {
        throw new Error(`Failed to update shape overlay "${id}" style`);
      }
    }
    if (options.transform !== undefined) {
      if (!store.updateShapeTransform(id, options.transform as Partial<Transform>)) {
        throw new Error(`Failed to update shape overlay "${id}" transform`);
      }
    }
    if (options.fullFrame) {
      const { width, height } = useProjectStore.getState().project.settings;
      const updated = store.updateShapeTransform(id, {
        position: { x: 0.5, y: 0.5 },
        scale: { x: width / 200, y: height / 200 },
        anchor: { x: 0.5, y: 0.5 },
        rotation: 0,
        opacity: 1,
      });
      if (!updated) throw new Error(`Failed to resize shape overlay "${id}"`);
    }
    const clip = useProjectStore.getState().getShapeClip(id);
    if (!clip) throw new Error(`Shape overlay "${id}" not found`);
    return { id, trackId: clip.trackId };
  }

  async createStickerOverlay(
    options: StickerOverlayOptions,
  ): Promise<OverlayRef> {
    this.requireOpenProject();
    const { stickerLibrary } = await import("@openreel/core");
    const created = await insertTimelineOverlay(
      options.startSec,
      options.durationSec,
      (trackId) => {
        const clip = options.imageUrl
          ? stickerLibrary.createStickerClip(
              {
                id: crypto.randomUUID(),
                name: options.name ?? "sticker",
                category: "custom",
                imageUrl: options.imageUrl,
              },
              trackId,
              options.startSec,
              options.durationSec,
            )
          : stickerLibrary.createEmojiClip(
              {
                id: crypto.randomUUID(),
                emoji: options.emoji ?? "⭐",
                name: options.name ?? options.emoji ?? "emoji",
                category: "emojis",
              },
              trackId,
              options.startSec,
              options.durationSec,
            );
        return useProjectStore.getState().createStickerClip(clip);
      },
      options.trackId,
    );
    if (!created) throw new Error("Failed to create sticker overlay");
    return { id: created.id, trackId: created.trackId };
  }

  async updateStickerOverlay(
    id: string,
    options: UpdateStickerOverlayOptions,
  ): Promise<OverlayRef> {
    this.requireOpenProject();
    const store = useProjectStore.getState();
    if (!store.getStickerClip(id)) {
      throw new Error(`Sticker overlay "${id}" not found`);
    }
    if (options.transform !== undefined) {
      if (!store.updateShapeTransform(id, options.transform as Partial<Transform>)) {
        throw new Error(`Failed to update sticker overlay "${id}" transform`);
      }
    }
    const clip = useProjectStore.getState().getStickerClip(id);
    if (!clip) throw new Error(`Sticker overlay "${id}" not found`);
    return { id, trackId: clip.trackId };
  }

  async createSvgOverlay(options: SvgOverlayOptions): Promise<OverlayRef> {
    this.requireOpenProject();
    const clip = await insertTimelineOverlay(
      options.startSec,
      options.durationSec,
      (trackId) =>
        useProjectStore
          .getState()
          .importSVG(
            options.svg,
            trackId,
            options.startSec,
            options.durationSec,
          ),
      options.trackId,
    );
    if (!clip) throw new Error("Failed to create SVG overlay");
    return { id: clip.id, trackId: clip.trackId };
  }

  async updateSvgOverlay(
    id: string,
    updates: Record<string, unknown>,
  ): Promise<OverlayRef> {
    this.requireOpenProject();
    const clip = useProjectStore.getState().updateSVGClip(id, updates);
    if (!clip) throw new Error(`SVG overlay "${id}" not found`);
    return { id, trackId: clip.trackId };
  }

  async removeOverlay(kind: OverlayKind, id: string): Promise<boolean> {
    this.requireOpenProject();
    const store = useProjectStore.getState();
    switch (kind) {
      case "text":
        return store.deleteTextClip(id);
      case "shape":
        return store.deleteShapeClip(id);
      case "sticker":
        return store.deleteStickerClip(id);
      case "svg":
        return store.deleteSVGClip(id);
      default:
        return false;
    }
  }
}