Skip to content

Open source CapCut alternative.

TypeScript

feat(site): compose generation prompts around their references

David Le71e5c8d1e2ac
diff --git a/site/src/cut/lib/composeGen.ts b/site/src/cut/lib/composeGen.ts
new file mode 100644
index 00000000..dd52a9e0
--- /dev/null
+++ b/site/src/cut/lib/composeGen.ts
@@ -0,0 +1,88 @@
+"use client";
+
+import { geminiModelRoles } from "@/lib/inference/gemini-models";
+import type { AssetRef } from "./assetRef";
+import { hostedPost } from "./hosted";
+import { readRefText, refsToParts, type InlineImage } from "./refMedia";
+
+// Reference-aware prompt composition, shared by video and image generation.
+//
+// The generators read their input images literally: Veo plays its one input
+// image as the video's first frame, and the image model edits the pixels it is
+// given. So a prompt that asks to *transform* a reference ("turn this into a
+// korean woman") loses to the picture, and text-file references (a script,
+// notes) can't ride as images at all. Before rendering, a small multimodal
+// Gemini call reads every reference — video frames, images, text contents —
+// plus the user's request, rewrites the prompt to stand alone, and picks which
+// pictures should still ride to the generator. Best-effort throughout: any
+// failure returns null and the caller falls back to the raw prompt + refs.
+
+const VIDEO_INSTRUCTIONS = `You prepare prompts for a text+image-to-video model. The numbered images and attached files are the user's references; their request comes last. The model accepts at most one input image and plays it as the video's literal first frame — it can animate what is pictured but cannot change who or what is in it.
+Reply with JSON {"prompt": string, "keepImages": number[]}:
+- keepImages lists at most one image number: the one whose exact pixels should open the video. Keep an image only when the request uses its subject as-is (animate it, continue the shot, have them speak). Keep none when the request transforms or replaces what any reference shows.
+- prompt must stand alone: fold in everything the video needs from every reference — setting, framing, lighting, wardrobe, camera, mood, action, any script or text from attached files — with the requested changes applied. Describe subjects concretely; if anyone speaks, give them a matching voice and the words to say.`;
+
+const IMAGE_INSTRUCTIONS = `You prepare prompts for an image generation and editing model. The numbered images and attached files are the user's references; their request comes last. The model receives the kept images as inputs it can edit, combine, and draw from.
+Reply with JSON {"prompt": string, "keepImages": number[]}:
+- keepImages lists the image numbers the model should receive: keep the ones the request edits, combines, or borrows a subject or style from; drop ones whose contribution is better said in words.
+- prompt must stand alone given only the kept images: refer to them by their content, and fold in whatever matters from the dropped references and any script or text from attached files, with the requested changes applied.`;
+
+export interface ComposedGen {
+ prompt: string;
+ /** The reference pictures the generator should receive, in kept order. */
+ images: InlineImage[];
+}
+
+/** Compose a generation prompt from the user's request and their references.
+ * Null on any failure (or when no ref contributes anything) — callers fall
+ * back to sending the raw prompt with the visual refs as-is. */
+export async function composeGenPrompt(
+ target: "video" | "image",
+ prompt: string,
+ refs: AssetRef[]
+): Promise<ComposedGen | null> {
+ try {
+ const { parts, visuals } = await refsToParts(refs);
+ if (parts.length === 0) return null;
+ const res = await hostedPost("/api/inference/responses", {
+ donkeyProvider: "gemini",
+ model: geminiModelRoles.chat,
+ instructions: target === "video" ? VIDEO_INSTRUCTIONS : IMAGE_INSTRUCTIONS,
+ response_format: { type: "json_object" },
+ input: [{ role: "user", content: [...parts, { text: `Request: ${prompt}` }] }],
+ });
+ if (!res.ok) return null;
+ const body = (await res.json()) as { output_text?: string };
+ const parsed = JSON.parse(body.output_text ?? "") as {
+ prompt?: unknown;
+ keepImages?: unknown;
+ };
+ if (typeof parsed.prompt !== "string" || !parsed.prompt.trim()) return null;
+ const keep = Array.isArray(parsed.keepImages)
+ ? parsed.keepImages.filter(
+ (n): n is number => typeof n === "number" && Number.isInteger(n) && n >= 1 && n <= visuals.length
+ )
+ : [];
+ return { prompt: parsed.prompt.trim(), images: keep.map((n) => visuals[n - 1]) };
+ } catch {
+ return null;
+ }
+}
+
+/** Fallback text folding for when the compose call fails: append each text
+ * ref's contents to the prompt so a dropped script still reaches the
+ * generator. Unreadable files are skipped. */
+export async function foldTextRefs(prompt: string, refs: AssetRef[]): Promise<string> {
+ const blocks = await Promise.all(
+ refs
+ .filter((r) => r.kind === "text")
+ .map((r) =>
+ readRefText(r).then(
+ (contents) => `Contents of "${r.name}":\n${contents}`,
+ () => null
+ )
+ )
+ );
+ const kept = blocks.filter((b): b is string => b !== null);
+ return kept.length > 0 ? `${prompt}\n\n${kept.join("\n\n")}` : prompt;
+}
diff --git a/site/src/cut/lib/generate.ts b/site/src/cut/lib/generate.ts
index 8d785e96..68db6c83 100644
--- a/site/src/cut/lib/generate.ts
+++ b/site/src/cut/lib/generate.ts
@@ -4,8 +4,10 @@ import { useEffect } from "react";
import { create } from "zustand";
import { apiFetch, apiJson } from "./api";
import type { AssetRef } from "./assetRef";
+import { composeGenPrompt, foldTextRefs } from "./composeGen";
+import { hostedPost } from "./hosted";
import { enrichAsset, importFileToProject } from "./media";
-import { refsToInlineImages } from "./refMedia";
+import { refsToInlineImages, visualRefs, type InlineImage } from "./refMedia";
import { useImageGen } from "./imageGen";
import { useEditor } from "./store";
import { mediaUrl, type MediaAsset } from "./types";
@@ -41,8 +43,9 @@ interface GenerateState {
/** Kick off a generation. Returns when it settles, resolving to the final
* job (status "done" with assetId, or "error" with a message) so the AI
* assistant can act on the result; the panel just fires and forgets.
- * `refs` are visual references — each uploads as an input image (a stock
- * image as-is, a video by a captured poster frame). */
+ * `refs` are references of any kind — images upload as-is, videos by a
+ * captured poster frame, text files by their contents folded into the
+ * prompt (see composeGen.ts). */
generateImage: (
projectId: string,
prompt: string,
@@ -50,6 +53,9 @@ interface GenerateState {
refs?: AssetRef[];
aspect?: "16:9" | "9:16" | "1:1";
resolution?: "1K" | "2K" | "4K";
+ /** Rewrite the prompt around the references before rendering (the
+ * default when refs are present) — see composeGen.ts. */
+ composeRefs?: boolean;
}
) => Promise<GenerateJob>;
generateVideo: (
@@ -58,9 +64,19 @@ interface GenerateState {
opts?: {
tier?: "fast" | "high";
durationSeconds?: number;
- /** Visual references; Veo takes one input image, so the first ref's
- * picture seeds the render. */
+ /** Composition shape; defaults to the project's orientation. */
+ aspect?: "16:9" | "9:16";
+ resolution?: "720p" | "1080p";
+ /** References of any kind — video, image, text file. Veo takes one
+ * input image, so at most one picture seeds the render. */
refs?: AssetRef[];
+ /** Rewrite the prompt around the references before rendering (the
+ * default when refs are present) — see composeGen.ts. Veo plays the
+ * input image as the literal first frame, so a prompt that transforms
+ * the reference must become a standalone description with the image
+ * dropped; the rewrite decides which. Character mode passes false — the
+ * poster seed is the point. */
+ composeRefs?: boolean;
/** Called once with the landed asset when the render completes and the
* project is still open — lets the AI place the clip after a background
* render it couldn't wait out (the assistant tool bridge caps at 2min). */
@@ -70,7 +86,6 @@ interface GenerateState {
dismiss: (id: string) => void;
}
-const CLIENT_ID = "donkey-cut";
const REFRESH_MS = 8000;
const VIDEO_DEADLINE_MS = 12 * 60_000;
@@ -131,15 +146,6 @@ interface GenerationResponse {
metadata?: Record<string, unknown>;
}
-/** POST one of Donkey's hosted inference routes with the user's session. */
-export const hostedPost = (path: string, body: unknown, signal?: AbortSignal) =>
- fetch(path, {
- method: "POST",
- headers: { "Content-Type": "application/json", "x-donkey-client-id": CLIENT_ID },
- body: JSON.stringify(body),
- signal,
- });
-
async function readError(res: Response, fallback: string): Promise<string> {
if (res.status === 401) return "Sign in to Donkey to generate media.";
const body = (await res.json().catch(() => null)) as {
@@ -171,6 +177,28 @@ const providerError = (error: unknown): string | null => {
return null;
};
+/** Resolve a generation's prompt + input images from its refs. Runs the shared
+ * compose step (see composeGen.ts) unless the caller opted out; on a compose
+ * failure, falls back to the visual refs as-is with text-ref contents folded
+ * into the prompt. `maxImages` caps what the generator accepts (Veo: 1). */
+async function promptAndImages(
+ target: "video" | "image",
+ prompt: string,
+ refs: AssetRef[],
+ compose: boolean,
+ maxImages: number
+): Promise<{ prompt: string; images: InlineImage[] }> {
+ if (refs.length === 0) return { prompt, images: [] };
+ if (compose) {
+ const composed = await composeGenPrompt(target, prompt, refs);
+ if (composed) return { prompt: composed.prompt, images: composed.images.slice(0, maxImages) };
+ }
+ return {
+ prompt: await foldTextRefs(prompt, refs),
+ images: await refsToInlineImages(visualRefs(refs).slice(0, maxImages)),
+ };
+}
+
function bytesFromBase64(b64: string): Uint8Array<ArrayBuffer> {
const bin = atob(b64);
const bytes = new Uint8Array(new ArrayBuffer(bin.length));
@@ -239,10 +267,19 @@ export const useGenerate = create<GenerateState>((set, get) => {
try {
const aspect = opts?.aspect ?? useEditor.getState().aspect;
const resolution = opts?.resolution ?? useImageGen.getState().resolution;
- const images = await refsToInlineImages(opts?.refs ?? []);
+ // The job (and the landed asset's name) keeps the user's own words;
+ // only the render sees the composed prompt. The image model takes
+ // several input images, so every kept reference rides along.
+ const { prompt: sent, images } = await promptAndImages(
+ "image",
+ prompt,
+ opts?.refs ?? [],
+ opts?.composeRefs !== false,
+ Infinity
+ );
const res = await hostedPost("/api/inference/assets", {
kind: "image",
- prompt,
+ prompt: sent,
...(images.length > 0 ? { inputs: { images } } : {}),
// The image model takes a real frame + detail via imageConfig; no prompt steering.
parameters: { aspectRatio: aspect, imageSize: resolution },
@@ -287,16 +324,24 @@ export const useGenerate = create<GenerateState>((set, get) => {
set((s) => ({ jobs: [job, ...s.jobs] }));
return (async () => {
try {
- // Veo seeds from a single first-frame image; extra refs would be dropped
- // silently, so send just the first.
- const images = await refsToInlineImages((opts?.refs ?? []).slice(0, 1));
+ // The job (and the landed asset's name) keeps the user's own words;
+ // only the render sees the composed prompt. Veo seeds from a single
+ // first-frame image, so at most one kept picture rides along.
+ const { prompt: sent, images } = await promptAndImages(
+ "video",
+ prompt,
+ opts?.refs ?? [],
+ opts?.composeRefs !== false,
+ 1
+ );
const res = await hostedPost("/api/inference/assets", {
kind: "video",
- prompt,
+ prompt: sent,
...(images.length > 0 ? { inputs: { images } } : {}),
parameters: {
tier: opts?.tier === "high" ? "high" : "fast",
- aspectRatio: useEditor.getState().aspect,
+ aspectRatio: opts?.aspect ?? useEditor.getState().aspect,
+ ...(opts?.resolution ? { resolution: opts.resolution } : {}),
...(opts?.durationSeconds ? { durationSeconds: opts.durationSeconds } : {}),
},
});