Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance asset naming and insertion logic in generation functions

konstantin-pauluscad0a98986bc
diff --git a/apps/web/src/components/genai/insert.tsx b/apps/web/src/components/genai/insert.tsx
index c602baf5..1d7aa7b4 100644
--- a/apps/web/src/components/genai/insert.tsx
+++ b/apps/web/src/components/genai/insert.tsx
@@ -2,10 +2,12 @@
* 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 { Root } from "@diffusionstudio/runtime";
+import { Ai, getNextName, Name, Root } from "@diffusionstudio/runtime";
+import { assetName } from "@diffusionstudio/assets";
import { findEmptyPlacement } from "@/engine/placement";
import { toast } from "somoto";
+import type { AssetRef } from "@diffusionstudio/jsx";
import type { DocumentEditor } from "@/engine/editor";
import type { Entity, World } from "koota";
@@ -31,6 +33,9 @@ export interface GenerationBox {
*
* Returns the entities, or nothing (having said so) when there is no project
* to insert into.
+ *
+ * `sources[index]` is the declaration variant `index` consumes; each element
+ * is named after the asset its generation produces, once it lands.
*/
export function insertGenerated(
world: World,
@@ -38,6 +43,7 @@ export function insertGenerated(
size: { width: number; height: number },
element: (box: GenerationBox, index: number) => unknown,
count = 1,
+ sources: AssetRef[] = [],
): Entity[] {
const root = world.get(Root);
const inserted: Entity[] = [];
@@ -51,7 +57,11 @@ export function insertGenerated(
for (let index = 0; index < count; index++) {
const x = Math.round(left + index * (size.width + GAP));
const [entity] = editor.insertElement(root, () => element({ x, y, ...size }, index));
- if (entity) inserted.push(entity);
+ if (entity) {
+ inserted.push(entity);
+ const ref = sources[index];
+ if (ref) nameAfterGeneration(world, editor, entity, ref);
+ }
}
}
@@ -65,3 +75,17 @@ export function insertGenerated(
editor.select(inserted);
return inserted;
}
+
+/**
+ * Names the element after the asset its declaration resolves to — the same
+ * name a library insert would give it — unless it got one in the meantime.
+ * The resolution is memoized by ref, so this rides the request the element's
+ * own src makes rather than asking for another; a failure is the asset
+ * system's to surface (see SourceError), not this one's.
+ */
+function nameAfterGeneration(world: World, editor: DocumentEditor, entity: Entity, ref: AssetRef): void {
+ world.get(Ai)?.resolve(ref).then((asset) => {
+ if (!entity.isAlive() || entity.get(Name)?.value) return;
+ editor.editProperty(entity, 'name', getNextName(world, assetName(asset).replace(/\.[^.]+$/, '')));
+ }).catch(() => undefined);
+}
diff --git a/apps/web/src/components/genai/use-generate-audio.tsx b/apps/web/src/components/genai/use-generate-audio.tsx
index 264bd33c..1656bdb5 100644
--- a/apps/web/src/components/genai/use-generate-audio.tsx
+++ b/apps/web/src/components/genai/use-generate-audio.tsx
@@ -27,7 +27,7 @@ export function useGenerateAudio() {
insertGenerated(world, editor, AUDIO_SIZE, (box) => (
<Audio src={src} x={box.x} y={box.y} width={box.width} height={box.height} />
- ));
+ ), 1, [src]);
};
return { generate: run } as const;
diff --git a/apps/web/src/components/genai/use-generate-image.tsx b/apps/web/src/components/genai/use-generate-image.tsx
index 11a0fac3..7214bf4d 100644
--- a/apps/web/src/components/genai/use-generate-image.tsx
+++ b/apps/web/src/components/genai/use-generate-image.tsx
@@ -42,7 +42,7 @@ export function useGenerateImage() {
<Rect keepAspectRatio x={box.x} y={box.y} width={box.width} height={box.height}>
<ImagePaint src={sources[index]} />
</Rect>
- ), config.count);
+ ), config.count, sources);
};
return { generate: run } as const;
diff --git a/apps/web/src/components/genai/use-generate-video.tsx b/apps/web/src/components/genai/use-generate-video.tsx
index 6d2403ba..fb4f906d 100644
--- a/apps/web/src/components/genai/use-generate-video.tsx
+++ b/apps/web/src/components/genai/use-generate-video.tsx
@@ -39,7 +39,7 @@ export function useGenerateVideo() {
<Rect keepAspectRatio x={box.x} y={box.y} width={box.width} height={box.height}>
<VideoPaint src={src} />
</Rect>
- ));
+ ), 1, [src]);
};
return { generate: run } as const;
diff --git a/apps/web/src/components/genai/use-generate-voice.tsx b/apps/web/src/components/genai/use-generate-voice.tsx
index a8e9591a..931f2d1f 100644
--- a/apps/web/src/components/genai/use-generate-voice.tsx
+++ b/apps/web/src/components/genai/use-generate-voice.tsx
@@ -28,7 +28,7 @@ export function useGenerateVoice() {
insertGenerated(world, editor, AUDIO_SIZE, (box) => (
<Audio src={src} x={box.x} y={box.y} width={box.width} height={box.height} />
- ));
+ ), 1, [src]);
};
return { generate: run } as const;
diff --git a/apps/web/src/engine/hud/hud-system.ts b/apps/web/src/engine/hud/hud-system.ts
index e0e48970..aafefc97 100644
--- a/apps/web/src/engine/hud/hud-system.ts
+++ b/apps/web/src/engine/hud/hud-system.ts
@@ -42,7 +42,7 @@ export function hudSystem(world: World): void {
drawSnapLines(world, ctx, resolution);
drawHoverOutlines(world, ctx, resolution);
- for (const entity of world.query(ChildOf(world.get(Root)!), Not(Culled), Not(Hidden), Name)) {
+ for (const entity of world.query(ChildOf(world.get(Root)!), Not(Culled), Not(Hidden))) {
drawHeader(world, ctx, entity, resolution);
}
diff --git a/packages/runtime/src/queries/hierarchy.ts b/packages/runtime/src/queries/hierarchy.ts
index ed0b71ec..5bfb1f28 100644
--- a/packages/runtime/src/queries/hierarchy.ts
+++ b/packages/runtime/src/queries/hierarchy.ts
@@ -94,7 +94,8 @@ export function getNodeLocalFrame(node: Entity): number {
export function getNextName(world: World, prefix: string): string {
let max = 0;
- const pattern = new RegExp(`^${prefix} (\\d+)$`);
+
+ const pattern = new RegExp(`^${prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')} (\\d+)$`);
for (const entity of world.query(Name)) {
const match = entity.get(Name)!.value.match(pattern);