Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance asset management by introducing `generationKey` in asset loading and schema definitions. Update related functions to utilize `generationKey` for improved caching and retrieval of transcript assets. Refactor documentation to clarify caching behavior in the asset library.

konstantin-paulus0d86b8fb12ac
diff --git a/apps/web/src/components/engine/api/assets.ts b/apps/web/src/components/engine/api/assets.ts
index 426c554e..e6624663 100644
--- a/apps/web/src/components/engine/api/assets.ts
+++ b/apps/web/src/components/engine/api/assets.ts
@@ -123,6 +123,7 @@ export function raiseIdCounter(world: EngineWorld, ids: Iterable<string>): void
type LoadAssetOptions = {
name?: string;
generationId?: string | null;
+ generationKey?: string | null;
folderId?: string | null;
};
@@ -264,6 +265,7 @@ export async function loadAsset(
size: file.size,
handle: fileHandle,
generationId: options?.generationId ?? null,
+ generationKey: options?.generationKey ?? null,
folderId: options?.folderId ?? null,
}
diff --git a/apps/web/src/components/engine/db/schemas.ts b/apps/web/src/components/engine/db/schemas.ts
index 088ad3dc..de8071c5 100644
--- a/apps/web/src/components/engine/db/schemas.ts
+++ b/apps/web/src/components/engine/db/schemas.ts
@@ -77,6 +77,7 @@ export const assetBaseSchema = z.object({
z.instanceof(ElectronFileHandle),
]),
generationId: z.string().nullable().optional(),
+ generationKey: z.string().nullable().optional(),
folderId: z.string().nullable().optional(),
});
@@ -114,7 +115,6 @@ export type VideoAsset = z.infer<typeof videoAssetSchema>;
export const transcriptAssetSchema = assetBaseSchema.extend({
type: z.literal('TRANSCRIPT'),
- sourceHash: z.string().optional(),
});
export type TranscriptAsset = z.infer<typeof transcriptAssetSchema>;
diff --git a/apps/web/src/components/engine/utils/captions.ts b/apps/web/src/components/engine/utils/captions.ts
index c3234243..db3d1554 100644
--- a/apps/web/src/components/engine/utils/captions.ts
+++ b/apps/web/src/components/engine/utils/captions.ts
@@ -4,7 +4,7 @@
import { hasComponent, entityExists } from "bitecs";
import { trpc } from "@/lib/trpc";
-import { PaintType, getEntityTree, getParentEntity, secondsToFrames, uploadBlob, loadAsset, saveAsset } from "@/components/engine";
+import { PaintType, getEntityTree, getParentEntity, secondsToFrames, uploadBlob, loadAsset } from "@/components/engine";
import { createEncoder } from "@/components/engine/encode/encoder";
import { resolveTranscript } from "@/components/engine/decoders/caption/utils";
import { assert } from "@/utils";
@@ -107,9 +107,9 @@ export async function transcribeScene(
throw new Error("No audio found. Add an audio or video clip to the scene to generate captions.");
}
- const sourceHash = sceneAudioFingerprint(world, sceneEid);
+ const generationKey = sceneAudioFingerprint(world, sceneEid);
const cached = Array.from(world.assets.values())
- .find(a => a.type === "TRANSCRIPT" && a.sourceHash === sourceHash);
+ .find(a => a.type === "TRANSCRIPT" && a.generationKey === generationKey);
if (cached) {
const transcript = await resolveTranscript(cached);
@@ -155,10 +155,9 @@ export async function transcribeScene(
const name = nextCaptionsName(Array.from(world.assets.values()));
const blob = new Blob([JSON.stringify(transcript)], { type: "application/json" });
const file = new File([blob], `${name}.json`, { type: "application/json" });
- const asset = await loadAsset(world, file, { name });
+ const asset = await loadAsset(world, file, { name, generationKey });
assert(asset.type === "TRANSCRIPT", "Expected a transcript asset");
- await saveAsset(world, { ...asset, sourceHash });
return { asset: { id: asset.id, name: asset.name, type: asset.type }, trim: transcriptTrim(transcript) };
}
diff --git a/apps/web/src/utils/jsx-generation.ts b/apps/web/src/utils/jsx-generation.ts
index faa0ac1d..22edd65f 100644
--- a/apps/web/src/utils/jsx-generation.ts
+++ b/apps/web/src/utils/jsx-generation.ts
@@ -48,7 +48,7 @@ const VOICE_MODEL = "elevenlabs-v3";
/**
* A spec with defaults applied and every `AssetInput` reduced to an asset id.
- * Field order is fixed, so `JSON.stringify` of it is a stable cache key.
+ * Field order is fixed, so `JSON.stringify` of it is a stable `generationKey`.
*/
type ResolvedSpec =
| { type: "image"; model: string; prompt: string; aspectRatio: AspectRatio; seed?: number; refIds: string[] }
@@ -57,11 +57,11 @@ type ResolvedSpec =
| { type: "audio"; model: string; prompt: string };
/**
- * Generated assets, cached by fully-resolved spec. Scoped per world so a
- * cached asset id always exists in the asset store it is reused from, and
- * per-session by construction (in-memory, never persisted).
+ * In-flight generations per world, keyed by `generationKey`, so identical
+ * concurrent declarations collapse to one request. Entries are removed once
+ * settled; finished generations are found via `asset.generationKey`.
*/
-const sessionCaches = new WeakMap<EngineWorld, Map<string, Promise<Asset>>>();
+const inflight = new WeakMap<EngineWorld, Map<string, Promise<Asset>>>();
/**
* Declarations already resolved within one `generateAssets` run, keyed by ref
@@ -85,25 +85,27 @@ function resolveInput(world: EngineWorld, input: AssetInput, memo: GenerationMem
async function generateFromRef(world: EngineWorld, ref: AssetRef, memo: GenerationMemo): Promise<Asset> {
const resolved = await resolveSpec(world, getAssetSpec(ref), memo);
- const key = JSON.stringify(resolved);
+ const generationKey = JSON.stringify(resolved);
- let cache = sessionCaches.get(world);
- if (!cache) {
- cache = new Map();
- sessionCaches.set(world, cache);
- }
+ const cached = Array.from(world.assets.values()).find((asset) => asset.generationKey === generationKey);
+ if (cached) return cached;
- const cached = cache.get(key);
- if (cached) {
- // Reuse unless the generation failed or its asset was deleted since.
- const asset = await cached.catch(() => null);
- if (asset && world.assets.has(asset.id)) return asset;
- cache.delete(key);
+ let pending = inflight.get(world);
+ if (!pending) {
+ pending = new Map();
+ inflight.set(world, pending);
}
- const promise = runGeneration(world, resolved);
- cache.set(key, promise);
- return await promise;
+ const running = pending.get(generationKey);
+ if (running) return await running;
+
+ const promise = runGeneration(world, resolved, generationKey);
+ pending.set(generationKey, promise);
+ try {
+ return await promise;
+ } finally {
+ pending.delete(generationKey);
+ }
}
async function resolveSpec(world: EngineWorld, spec: AssetSpecInput, memo: GenerationMemo): Promise<ResolvedSpec> {
@@ -169,12 +171,12 @@ function checkVideoConstraints(spec: Extract<ResolvedSpec, { type: "video" }>):
assert(spec.endFrameId === undefined || model.features.includes("end-frame"), `${spec.model} does not support an end frame`);
}
-async function runGeneration(world: EngineWorld, spec: ResolvedSpec): Promise<Asset> {
+async function runGeneration(world: EngineWorld, spec: ResolvedSpec, generationKey: string): Promise<Asset> {
const { name, results, generationId } = await requestGeneration(world, spec);
assert(results.length > 0, "No results returned from the model");
const folderId = await ensureGenerationFolder(world, name, 1);
- return await loadAsset(world, results[0].url, { name, generationId, folderId });
+ return await loadAsset(world, results[0].url, { name, generationId, generationKey, folderId });
}
function requestGeneration(world: EngineWorld, spec: ResolvedSpec) {
diff --git a/docs/composition.md b/docs/composition.md
index 26b7f09b..5389127b 100644
--- a/docs/composition.md
+++ b/docs/composition.md
@@ -144,7 +144,7 @@ Run `dapi models <type>` for valid model ids and per-model constraints, `dapi vo
Declarations are pure: nothing generates until the mount commits, and refs never used by a mounted element are dropped. Because `startFrame`/`endFrame`/`refs` take refs as values, dependencies generate in the right order and cycles are impossible.
-**Caching:** results are cached by content (model, prompt, resolved refs, seed, …) within a session: re-mounting an unchanged project reuses assets instead of regenerating, and identical declarations collapse to one asset. Set `seed` for reproducibility.
+**Caching:** results are cached by content (model, prompt, resolved refs, seed, …) in the asset library: re-mounting an unchanged project reuses its assets instead of regenerating, even across sessions, and identical declarations collapse to one asset. Deleting a generated asset clears its cache entry. Set `seed` for reproducibility.
## Captions