Skip to content

The VS Code of video editing, built for agents.

TypeScript

Implement project bundle management and enhance database schema

konstantin-paulusdc614bb3b8f3
diff --git a/apps/web/src/components/dashboard/projects-view.tsx b/apps/web/src/components/dashboard/projects-view.tsx
index b9632a5f..0fb79a0a 100644
--- a/apps/web/src/components/dashboard/projects-view.tsx
+++ b/apps/web/src/components/dashboard/projects-view.tsx
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { generateProjectName } from "@/components/engine/db";
+import { forgetProjectBundle, generateProjectName } from "@/components/engine/db";
import {
ContextMenu,
ContextMenuContent,
@@ -121,6 +121,7 @@ export function DashboardProjectsView() {
const handleDelete = async (project: ProjectInfo) => {
try {
await deleteProject(project.dir);
+ forgetProjectBundle(project.id);
track('project_deleted');
setSelectedProject((current) => (current === project.dir ? null : current));
refetchProjects();
diff --git a/apps/web/src/components/engine/db/global-db.ts b/apps/web/src/components/engine/db/global-db.ts
index 438a11a0..87fd64d1 100644
--- a/apps/web/src/components/engine/db/global-db.ts
+++ b/apps/web/src/components/engine/db/global-db.ts
@@ -33,14 +33,25 @@ export function generateProjectName(): string {
*/
export interface ProjectRoot {
id: string;
- /** Absolute path of the folder. */
- path: string;
- /** Its last segment, for showing it. */
+ path: string; // Absolute path of the folder.
name: string;
createdAt: string;
lastUsedAt: string;
}
+/**
+ * The bundle a project last mounted successfully, keyed by its id. Two jobs:
+ * the copy an export re-renders (see `@/engine/capture`), and the head start
+ * the next open of the project mounts while its first compile still runs.
+ * Written only after a mount lands, so what is here is always something the
+ * canvas has shown.
+ */
+export interface ProjectBundle {
+ projectId: string;
+ code: string;
+ updatedAt: string;
+}
+
export interface GlobalDBSchema extends idb.DBSchema {
roots: {
value: ProjectRoot;
@@ -50,10 +61,14 @@ export interface GlobalDBSchema extends idb.DBSchema {
'by-last-used': string;
};
};
+ bundles: {
+ value: ProjectBundle;
+ key: string;
+ };
}
const DB_NAME = 'diffusion-studio-idb';
-const DB_VERSION = 1;
+const DB_VERSION = 2;
const dbPromise = openDB<GlobalDBSchema>(DB_NAME, DB_VERSION, {
upgrade(db) {
@@ -62,6 +77,9 @@ const dbPromise = openDB<GlobalDBSchema>(DB_NAME, DB_VERSION, {
store.createIndex('by-path', 'path', { unique: true });
store.createIndex('by-last-used', 'lastUsedAt');
}
+ if (!db.objectStoreNames.contains('bundles')) {
+ db.createObjectStore('bundles', { keyPath: 'projectId' });
+ }
},
});
@@ -111,3 +129,39 @@ export async function forgetProjectRoot(id: string): Promise<void> {
const db = await dbPromise;
await db.delete('roots', id);
}
+
+// ---------------------------------------------------------------------------
+// Project bundles
+
+/** Records the bundle `projectId` just mounted, replacing the one before it. */
+export async function rememberProjectBundle(projectId: string, code: string): Promise<void> {
+ try {
+ const db = await dbPromise;
+ await db.put('bundles', { projectId, code, updatedAt: new Date().toISOString() });
+ } catch (e) {
+ console.error('Failed to remember project bundle', e);
+ }
+}
+
+/** The bundle `projectId` last mounted, or null when it has none on record. */
+export async function loadProjectBundle(projectId: string): Promise<string | null> {
+ if (!projectId) return null;
+ try {
+ const db = await dbPromise;
+ return (await db.get('bundles', projectId))?.code ?? null;
+ } catch (e) {
+ console.error('Failed to load project bundle', e);
+ return null;
+ }
+}
+
+/** Forgets a project's bundle, for when the project itself is deleted. */
+export async function forgetProjectBundle(projectId: string): Promise<void> {
+ if (!projectId) return;
+ try {
+ const db = await dbPromise;
+ await db.delete('bundles', projectId);
+ } catch (e) {
+ console.error('Failed to forget project bundle', e);
+ }
+}
diff --git a/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx b/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx
index eed29ac0..c312d5e1 100644
--- a/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx
+++ b/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx
@@ -15,7 +15,7 @@ import {
import { useNavigate } from "@solidjs/router";
import { For, Show, createMemo } from "solid-js";
import { toast } from "somoto";
-import { generateProjectName } from "@/components/engine/db";
+import { forgetProjectBundle, generateProjectName } from "@/components/engine/db";
import { createProject, deleteProject, duplicateProject, ensureProjectsRoot } from "@/projects";
import { AssetId, ChildOf, Name, Root, Scene, getAssetFile, getActiveEntity, sortByItemIndex } from "@diffusionstudio/runtime";
import { assetName } from "@diffusionstudio/assets";
@@ -59,6 +59,7 @@ export function FileMenu() {
const handleDeleteProject = async () => {
try {
await deleteProject(project.dir());
+ forgetProjectBundle(project.id());
navigate("/?dashboard=projects");
} catch (e) {
toast.error("Failed to delete project", { description: (e as Error).message });
diff --git a/apps/web/src/context/render.ts b/apps/web/src/context/render.ts
index fa618258..68ee13ca 100644
--- a/apps/web/src/context/render.ts
+++ b/apps/web/src/context/render.ts
@@ -68,7 +68,7 @@ export async function renderScene(
let capture: Capture | undefined;
try {
- capture = createCapture(world, scene, {
+ capture = await createCapture(world, scene, {
frameRate: config?.video?.fps,
mode: config?.video?.enabled === false || config?.format === "ogg" ? "offline-audio" : "offline-video",
});
diff --git a/apps/web/src/engine/capture.ts b/apps/web/src/engine/capture.ts
index 0cebd8af..9f1a1eba 100644
--- a/apps/web/src/engine/capture.ts
+++ b/apps/web/src/engine/capture.ts
@@ -27,7 +27,7 @@ import {
createRuntimeWorld, disposeDecoders, getParentNode, removeChild, resetCamera,
} from '@diffusionstudio/runtime';
-import { ProjectBundle } from './traits';
+import { loadProjectBundle } from '@/components/engine/db';
import type { RuntimeMode } from '@diffusionstudio/runtime';
import type { Entity, World } from 'koota';
@@ -55,8 +55,8 @@ export interface Capture {
* when the node has no source stamp yet (an element the writer has not put in
* the file), or when rendering the project produces nothing for that stamp.
*/
-export function createCapture(source: World, node: Entity, options: CaptureOptions = {}): Capture {
- const code = source.get(ProjectBundle)?.code;
+export async function createCapture(source: World, node: Entity, options: CaptureOptions = {}): Promise<Capture> {
+ const code = await loadProjectBundle(source.get(Project)?.id ?? '');
if (!code) throw new Error('There is no project to render');
// The stamp, not the entity: the two worlds hand out ids of their own, and
diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.ts
index 7d028845..30abafe6 100644
--- a/apps/web/src/engine/create-engine.ts
+++ b/apps/web/src/engine/create-engine.ts
@@ -5,7 +5,7 @@
import { assetSystem, renderSystem, transformSystem, playbackSystem, motionSystem, AudioEngine, createRuntimeWorld, Geometry, Mode, RenderSurface, Time, ChildOf, syncInteractiveState } from '@diffusionstudio/runtime';
import { hudSystem } from './hud';
import { createSignal, type Accessor, type Setter } from 'solid-js';
-import { AssetSelection, Hud, Keys, Pointer, PointerEvents, ProjectBundle, ProjectConfig, SnapLines } from './traits';
+import { AssetSelection, Hud, Keys, Pointer, PointerEvents, ProjectConfig, SnapLines } from './traits';
import { inputSystem } from './input/input-system';
import { clearClipFrames, clearClipPeaks, clearMedia, clearPeaks, timelineSystem, TimelineSurface } from './timeline';
import { shortcutSystem } from './input/shortcuts';
@@ -49,7 +49,7 @@ class Engine {
public constructor(projectId: string, options: EngineOptions = {}) {
this.world = createRuntimeWorld(projectId);
- this.world.add(Pointer, Keys, SnapLines, Hud, PointerEvents, AssetSelection, ProjectConfig, ProjectBundle, TimelineSurface);
+ this.world.add(Pointer, Keys, SnapLines, Hud, PointerEvents, AssetSelection, ProjectConfig, TimelineSurface);
this.unsubscribe.push(
this.world.onAdd(ChildOf('*'), () => (this.interactiveDirty = true)),
diff --git a/apps/web/src/engine/traits.ts b/apps/web/src/engine/traits.ts
index 663870ce..92a341c9 100644
--- a/apps/web/src/engine/traits.ts
+++ b/apps/web/src/engine/traits.ts
@@ -82,14 +82,3 @@ export const AssetSelection = trait({ id: null as string | null });
* like Library: the values are its own reactive state.
*/
export const ProjectConfig = trait(() => null as ProjectConfigStore | null);
-
-/**
- * The compiled bundle the world's project is mounted from. Kept because a
- * render of it is not reproducible from the entities alone: what a
- * `<surface>` draws, what an `<html>` subtree shows and what a `useTicker`
- * memo feeds only exist while the project's graph runs, so an export renders
- * the same bundle a second time into its offline world (see
- * `@/context/render`). Empty until the first mount lands; a compile that
- * fails leaves the last good one, the way the canvas keeps the last render.
- */
-export const ProjectBundle = trait({ code: '' });
diff --git a/apps/web/src/pages/editor.tsx b/apps/web/src/pages/editor.tsx
index 5828372a..af3d1951 100644
--- a/apps/web/src/pages/editor.tsx
+++ b/apps/web/src/pages/editor.tsx
@@ -10,7 +10,7 @@ import { FloatingProjectHeader, SidebarLeft } from "@/components/sidebar-left";
import { useLayout, MIN_TIMELINE_HEIGHT } from "@/context/layout";
import { useEditorApi } from "@/context/dapi";
import { RULER_HEIGHT } from "@/engine/timeline";
-import { createEffect, onCleanup } from 'solid-js';
+import { createEffect, onCleanup, untrack } from 'solid-js';
import { toast } from 'somoto';
import { useWorld } from '@diffusionstudio/koota-solid';
import { mount } from '@diffusionstudio/reconciler';
@@ -18,7 +18,7 @@ import { getDocumentEditor } from '@/engine/editor';
import { attachLibrary, isLibraryFile } from '@/engine/library';
import { attachAi } from '@/utils/gen-ai';
import { attachProjectConfig, isProjectConfigFile } from '@/engine/project-config';
-import { ProjectBundle } from '@/engine/traits';
+import { loadProjectBundle, rememberProjectBundle } from '@/components/engine/db';
import { isCacheFile } from '@diffusionstudio/assets';
import { createEditWriter } from '@/projects/edits';
import { compileProject, watchProject } from '@/projects/host';
@@ -47,6 +47,7 @@ export function EditorPage() {
if (!dir) return;
let mounted: Mount | undefined;
+ let mountedCode: string | undefined;
let writer: EditWriter | undefined;
let unlisten: (() => void) | undefined;
let disposed = false;
@@ -68,11 +69,52 @@ export function EditorPage() {
writer = undefined;
mounted?.dispose();
mounted = undefined;
+ mountedCode = undefined;
+ };
+
+ /** Puts `code` on the stage, unless it is what is there already. */
+ const applyBundle = (code: string): void => {
+ if (code === mountedCode) return;
+ // The old render goes first: there is only one stage per world.
+ unmount();
+ mounted = mount(code, world);
+ mountedCode = code;
+ // The rendered scene knows which element every entity came from, so
+ // from here on an edit in the editor can find its way back.
+ writer = createEditWriter(dir, world);
+ const editor = getDocumentEditor(world);
+ unlisten = editor.onEdit((edit) => writer?.push(edit));
};
const loadProject = async (): Promise<void> => {
const current = ++generation;
- const [result] = await Promise.all([compileProject(dir), library.load()]);
+ const compiling = compileProject(dir);
+ const loading = library.load();
+
+ // First open only: the bundle the last session mounted, straight from
+ // the app's database, goes on the stage while the compile chews
+ // through the sources — unless the compile wins the race outright. A
+ // bundle the sources have outgrown can fail against today's assets;
+ // the compile that is already running replaces it either way.
+ if (current === 1) {
+ // Neither arm may reject: the loser would be an unhandled rejection,
+ // and the compile's real failure is dealt with below.
+ const cached = await Promise.race([
+ Promise.all([loadProjectBundle(untrack(project.id)), loading])
+ .then(([code]) => code, () => null),
+ compiling.then(() => null, () => null),
+ ]);
+ if (disposed || current !== generation) return;
+ if (cached && mountedCode === undefined) {
+ try {
+ applyBundle(cached);
+ } catch {
+ // The compile lands next, with a toast of its own if it must.
+ }
+ }
+ }
+
+ const [result] = await Promise.all([compiling, loading]);
if (disposed || current !== generation) return;
// A broken edit keeps the last good render on the canvas.
@@ -81,17 +123,13 @@ export function EditorPage() {
return;
}
- // The old render goes first: there is only one stage per world.
- unmount();
try {
- mounted = mount(result.code, world);
- // What an export renders a second time, for the one scene it is of.
- world.set(ProjectBundle, { code: result.code });
- // The rendered scene knows which element every entity came from, so
- // from here on an edit in the editor can find its way back.
- writer = createEditWriter(dir, world);
- const editor = getDocumentEditor(world);
- unlisten = editor.onEdit((edit) => writer?.push(edit));
+ applyBundle(result.code);
+ // What an export renders a second time, and the next open's head
+ // start (see `rememberProjectBundle`) — recorded only once it has
+ // actually mounted, so the record never runs ahead of the canvas.
+ rememberProjectBundle(untrack(project.id), result.code).catch((error) =>
+ console.error('[projects] could not save the bundle', error));
} catch (error) {
toast.error('Project failed to render', { description: (error as Error).message });
}
diff --git a/apps/web/src/utils/gen-ai.ts b/apps/web/src/utils/gen-ai.ts
index d19f5f19..c3c951f5 100644
--- a/apps/web/src/utils/gen-ai.ts
+++ b/apps/web/src/utils/gen-ai.ts
@@ -148,7 +148,7 @@ export class EditorGenAi extends GenAi {
// The scene's own capture world: the project rendered again, reduced to
// this scene, with nothing drawn — see `createCapture`.
- const capture = createCapture(world, scene, { mode: "offline-audio" });
+ const capture = await createCapture(world, scene, { mode: "offline-audio" });
let result: ExportResult;
try {
const encoder = await createEncoder(capture.world, {