Skip to content

Open source CapCut alternative.

TypeScript

feat(site): audio attachments ride inline to the Cut chat model [rebuild]

David Lee5c2c63c7dc1
diff --git a/site/src/cut/lib/geminiChat.ts b/site/src/cut/lib/geminiChat.ts
index f1523543..7edda97e 100644
--- a/site/src/cut/lib/geminiChat.ts
+++ b/site/src/cut/lib/geminiChat.ts
@@ -31,7 +31,7 @@ const toolDeclarations = () =>
/** The conversation replayed as hosted-Responses input items. Only text (and
* attachment refs) from past turns — tool traffic stays within its own turn.
* The fresh editor snapshot rides on the newest user message alone, and so do
- * the attachments' actual payloads (video frames, images, text-file contents):
+ * the attachments' actual payloads (video frames, images, audio, text-file contents):
* older turns keep just the metadata JSON so replays stay within budget. */
async function inputFromMessages(messages: UIMessage[]): Promise<Item[]> {
const lastUser = messages.findLast((m) => m.role === "user");
diff --git a/site/src/cut/lib/refMedia.ts b/site/src/cut/lib/refMedia.ts
index f5f7a57a..15d0a1c9 100644
--- a/site/src/cut/lib/refMedia.ts
+++ b/site/src/cut/lib/refMedia.ts
@@ -56,15 +56,39 @@ function splitDataUrl(dataUrl: string): InlineImage {
return { data, mimeType };
}
-function blobToInline(blob: Blob): Promise<InlineImage> {
+function blobToDataUrl(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const r = new FileReader();
- r.onload = () => resolve(splitDataUrl(String(r.result)));
- r.onerror = () => reject(new Error("Could not read the image."));
+ r.onload = () => resolve(String(r.result));
+ r.onerror = () => reject(new Error("Could not read the file."));
r.readAsDataURL(blob);
});
}
+async function blobToInline(blob: Blob): Promise<InlineImage> {
+ return splitDataUrl(await blobToDataUrl(blob));
+}
+
+// Keeps the inline payload well under the Gemini per-request inline-data cap
+// (base64 inflates by 4/3); larger audio degrades to a name-only marker.
+const MAX_AUDIO_BYTES = 12 * 1024 * 1024;
+
+/** The audio bytes for `ref`, base64 with a real audio mime, or null when the
+ * file is too large to ride inline. */
+async function refToInlineAudio(ref: AssetRef): Promise<InlineImage | null> {
+ const res = await fetch(ref.url);
+ if (!res.ok) throw new Error(`Could not load “${ref.name}”.`);
+ const blob = await res.blob();
+ if (blob.size > MAX_AUDIO_BYTES) return null;
+ const dataUrl = await blobToDataUrl(blob);
+ const comma = dataUrl.indexOf(",");
+ const labelled = dataUrl.slice(5, comma).split(";")[0];
+ return {
+ data: dataUrl.slice(comma + 1),
+ mimeType: labelled.startsWith("audio/") ? labelled : "audio/mpeg",
+ };
+}
+
function captureFrame(url: string, duration?: number): Promise<InlineImage> {
return new Promise((resolve, reject) => {
const v = document.createElement("video");
@@ -172,8 +196,10 @@ export async function readRefText(ref: AssetRef): Promise<string> {
/** A ref list as hosted-Responses content parts, for any Gemini call that
* should actually see the attachments: each visual ref contributes a numbered
* label plus its picture (image as-is, video by poster frame), each text ref
- * its contents, and audio a name-only marker. `visuals` returns the pictures
- * in label order (`Image 1` = visuals[0]) so a model can pick among them. */
+ * its contents, and each audio ref its actual sound (so the model can answer
+ * about what's said without touching the project; oversized files fall back
+ * to a name-only marker). `visuals` returns the pictures in label order
+ * (`Image 1` = visuals[0]) so a model can pick among them. */
export async function refsToParts(
refs: AssetRef[]
): Promise<{ parts: Record<string, unknown>[]; visuals: InlineImage[] }> {
@@ -181,7 +207,13 @@ export async function refsToParts(
const visuals: InlineImage[] = [];
for (const ref of refs) {
if (ref.kind === "audio") {
- parts.push({ text: `Attached audio "${ref.name}" (audio content not included).` });
+ const audio = await refToInlineAudio(ref);
+ if (!audio) {
+ parts.push({ text: `Attached audio "${ref.name}" (too large to include inline).` });
+ continue;
+ }
+ parts.push({ text: `Attached audio "${ref.name}":` });
+ parts.push({ type: "input_audio", dataBase64: audio.data, mimeType: audio.mimeType });
continue;
}
if (ref.kind === "text") {
diff --git a/site/src/cut/server/ai/catalog.ts b/site/src/cut/server/ai/catalog.ts
index 3a25cf05..4abc8a80 100644
--- a/site/src/cut/server/ai/catalog.ts
+++ b/site/src/cut/server/ai/catalog.ts
@@ -612,7 +612,7 @@ Rules:
- Don't transcribe a video with no speech. If the user wants captions on silent footage, use subtitles_from_visuals (it narrates what's on screen). Never invent a spoken transcript.
- Voiceovers duck other audio by default so they stay audible. Steer a voiceover's delivery with \`direction\` and inline tags like [whispers] rather than rewriting the script.
- generate_image / generate_video / generate_character_video / voiceover_generate / read_subtitles_aloud make media through hosted models (spends the user's Donkey credits, needs sign-in); call them when the user asked for the media itself — a request for the prompt or script gets text in chat. Bundled stock media (stock_search / stock_add) is free — use it when it fits. Media the user attached is in \`media\`; pass those asset ids as generation references when they say "use this". Generated media previews on a chat card the user can expand, drag in, or file away; add it to the timeline (add_to_timeline:true or an index) only when they asked for it in the cut. Write a rich, specific prompt from their shorthand. Video renders take a minute or two.
-- You can see and hear: watch_video samples a source's frames into timestamped contact sheets (scene changes included) — watch before cutting footage you haven't seen; detect_silence finds dead air; capture_frame shows the one rendered frame at the playhead. The watching-and-cutting skill has the flow.`;
+- You can see and hear: audio the user attaches to their message plays right in it — answer "what does this say" from what you hear, no tool call. For project footage, watch_video samples a source's frames into timestamped contact sheets (scene changes included) — watch before cutting footage you haven't seen; detect_silence finds dead air; capture_frame shows the one rendered frame at the playhead. The watching-and-cutting skill has the flow.`;
}
export const AI_SKILL_INDEX = Object.keys(AI_SKILLS);