Open source CapCut alternative.
TypeScript
feat(site): generate music with Gemini/Lyria over the inference gateway
David Le37044672f118
diff --git a/site/src/app/api/inference/assets/route.ts b/site/src/app/api/inference/assets/route.tsindex ec95b8d1..f8a2515a 100644--- a/site/src/app/api/inference/assets/route.ts+++ b/site/src/app/api/inference/assets/route.ts@@ -25,6 +25,11 @@ import { InferenceProviderError } from "@/lib/inference/providers";export const dynamic = "force-dynamic";+// Music generation (Gemini/Lyria) polls its render to completion in-request, so+// the handler can stay open longer than the platform default. Image, video, and+// speech return well inside this; it is a ceiling, not a delay.+export const maxDuration = 120;+export const POST = withDonkeyAuth(async (request) => {const client = requireInferenceClientId(request.donkey.clientId);if (!client.ok) {diff --git a/site/src/lib/credits/provider-pricing.ts b/site/src/lib/credits/provider-pricing.tsindex 9ba9d353..16259200 100644--- a/site/src/lib/credits/provider-pricing.ts+++ b/site/src/lib/credits/provider-pricing.ts@@ -8,9 +8,11 @@ import {} from "@/lib/inference/elevenlabs-models";import {geminiModels,+ geminiMusicModels,geminiOmniModels,geminiTtsModels,type GeminiModel,+ type GeminiMusicModel,type GeminiOmniModel,type GeminiTtsModel,} from "@/lib/inference/gemini-models";@@ -57,6 +59,10 @@ export function providerCreditPricing(if (normalizedProvider === "gemini-omni") {return geminiOmniCreditPricing(normalizedModel);}+ // Lyria music ids are hardcoded (gemini-models.ts); a clip bills flat per render.+ if (normalizedProvider === "gemini-music") {+ return geminiMusicCreditPricing(normalizedModel);+ }if (normalizedProvider === "elevenlabs") {return elevenLabsCreditPricing(normalizedModel);}@@ -320,6 +326,29 @@ function geminiOmniCreditPricing(model: string): ProviderCreditPricing | undefinreturn geminiOmniModelPricing[model as GeminiOmniModel];}+// Generative music (Gemini/Lyria) bills flat per rendered clip: the render is a+// fixed-length interaction that carries no per-second usage, so a clip charges+// generationCount = 1 at completion. Rates are Lyria 3's published per-request+// prices ($0.04 clip, $0.08 full song); usdWithMargin adds the 1.3x. The Record+// is keyed by GeminiMusicModel, so adding a Lyria id without a price fails the build.+const geminiMusicModelPricing: Record<GeminiMusicModel, ProviderCreditPricing> = {+ [geminiMusicModels.clip]: { generationCostMicros: usdWithMargin("0.04") },+ [geminiMusicModels.pro]: { generationCostMicros: usdWithMargin("0.08") },+};++function geminiMusicCreditPricing(model: string): ProviderCreditPricing | undefined {+ const exact = geminiMusicModelPricing[model as GeminiMusicModel];+ if (exact) {+ return exact;+ }+ for (const [id, pricing] of Object.entries(geminiMusicModelPricing)) {+ if (modelMatches(model, id)) {+ return pricing;+ }+ }+ return undefined;+}+function geminiCreditPricing(model: string): ProviderCreditPricing | undefined {// Exact match wins, so a specific id is never shadowed by a broader prefix entry (e.g. an image// model "…-flash-image" under the "…-flash" text prefix). For dated/snapshot ids that only matchdiff --git a/site/src/lib/inference/adapters/gemini-music.ts b/site/src/lib/inference/adapters/gemini-music.tsnew file mode 100644index 00000000..27d0c2da--- /dev/null+++ b/site/src/lib/inference/adapters/gemini-music.ts@@ -0,0 +1,355 @@+import { GoogleGenAI } from "@google/genai";+import type { GoogleGenAIOptions, Interactions } from "@google/genai";++type Interaction = Interactions.Interaction;++import {+ constructGeminiClient,+ geminiApiError,+ geminiClientConfig,+ stringValue,+ type AdapterEnvironment,+} from "@/lib/inference/adapters/gemini-client";+import { providerCreditPricing } from "@/lib/credits/provider-pricing";+import { geminiModels, geminiMusicModels } from "@/lib/inference/gemini-models";+import { ensureConfigured } from "@/lib/inference/http";+import { isJsonObject, toJsonObject } from "@/lib/inference/json";+import {+ InferenceProviderError,+ type AssetGenerationProviderRequest,+ type AssetGenerationProviderResult,+ type GenerationOutputRef,+ type InferenceModality,+ type InferenceModel,+ type InferenceProvider,+ type JsonObject,+ type StoredGenerationForProvider,+} from "@/lib/inference/providers";++// The music generation provider (Gemini/Lyria) on the Interactions API: one call+// takes a text prompt and renders an instrumental clip — the background bed for a+// cut. Same Interactions surface as the Omni video adapter and the same Vertex+// service-account client (the legacy Lyria interaction models run on Vertex,+// unlike Lyria RealTime, the live WebSocket API, which Vertex rejects).+//+// Lyria renders synchronously — it rejects background interactions, so the create+// call blocks until the clip is ready and returns the finished audio inline (like+// the speech adapter). generateAsset still tolerates an in_progress result by+// polling, as a safety net. The provider id keeps refresh routing separate from+// the other Gemini adapters. Note: Lyria takes no response_format (a music model+// defaults to audio; passing one 400s) and filters prompts strictly.+const providerID = "gemini-music";++// How long generateAsset waits for a clip before giving up, and how often it+// polls. Bounded under the shared client's 120s request timeout so a stuck+// render can't hold the route open forever; a clip renders well inside it.+const MAX_WAIT_MS = 110_000;+const POLL_MS = 4_000;++// The clip model's fixed length; a requested bed longer than this renders on the+// pro model instead so the placed (trimmed) track can still span the video.+const CLIP_SECONDS = 30;++const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));++// `models` is used to rewrite a policy-blocked prompt into a safe description.+export type GeminiMusicClient = Pick<GoogleGenAI, "interactions" | "models">;+export type GeminiMusicClientFactory = (options: GoogleGenAIOptions) => GeminiMusicClient;++const defaultMusicClientFactory: GeminiMusicClientFactory = (options) =>+ constructGeminiClient(+ options,+ (opts) => new GoogleGenAI(opts),+ // Realize the interactions (NextGen) sub-client while the API-key env vars are+ // scrubbed, so its auth binds to the Vertex JWT instead of a stray env key.+ (client) => {+ void client.interactions;+ },+ );++export function createGeminiMusicAssetProvider(+ environment: AdapterEnvironment = process.env,+ clientFactory: GeminiMusicClientFactory = defaultMusicClientFactory,+): InferenceProvider {+ const clientConfig = geminiClientConfig(environment);+ const configured = clientConfig.configured;++ async function listModels(+ modalities: InferenceModality[],+ ): Promise<InferenceModel[]> {+ if (!modalities.includes("music") && !modalities.includes("audio")) {+ return [];+ }+ return [geminiMusicModels.clip, geminiMusicModels.pro].map((id) => ({+ id,+ name: id,+ provider: providerID,+ inputModalities: ["text"],+ outputModalities: ["music"],+ contextLength: null,+ pricing: null,+ metadata: { provider: providerID, api: "interactions" },+ }));+ }++ // The clip model renders a fixed short clip; the pro model a full-length track.+ // The Audio-tab generator steers length with a neutral `variant` ("clip"|"song");+ // the brief-to-video bed instead posts the video's `durationSeconds` with no+ // variant — so pick the longer pro model past the clip's fixed length, letting+ // the placed (and trimmed) bed span the video. Model ids stay in this adapter.+ function resolveModel(requested: string | undefined, parameters: JsonObject): string {+ const variant = stringValue(parameters.variant);+ const durationSeconds =+ typeof parameters.durationSeconds === "number" ? parameters.durationSeconds : undefined;+ const long =+ variant === "song" ||+ (variant === undefined && durationSeconds !== undefined && durationSeconds > CLIP_SECONDS);+ const model = requested?.trim() || (long ? geminiMusicModels.pro : geminiMusicModels.clip);+ // Fail before spending: the resolved model must have a configured price.+ if (!providerCreditPricing(providerID, model)) {+ throw new InferenceProviderError(+ "No credit price is configured for the selected music model.",+ { statusCode: 500, code: "music_model_not_priced", details: { model } },+ );+ }+ return model;+ }++ async function generateAsset({+ generationId,+ request,+ }: AssetGenerationProviderRequest): Promise<AssetGenerationProviderResult> {+ ensureConfigured(configured);++ if (request.kind !== "music") {+ throw new InferenceProviderError("Provider does not support this asset kind.", {+ statusCode: 400,+ code: "unsupported_asset_kind",+ });+ }++ const parameters = toJsonObject(request.parameters ?? {});+ const model = resolveModel(request.model, parameters);+ const prompt = request.prompt?.trim();+ if (!prompt) {+ throw new InferenceProviderError("Music generation requires a prompt.", {+ statusCode: 400,+ code: "empty_music_request",+ });+ }++ const client = clientFactory(clientConfig.options);+ let interaction: Interaction;+ try {+ // No background and no response_format: Lyria rejects both. The create call+ // blocks until the clip renders and comes back completed.+ interaction = await client.interactions.create({ model, input: prompt });+ } catch (error) {+ // Lyria blocks prompts that name a real artist, band, song, or brand.+ // Rather than fail, have a model rewrite the ask into a description of the+ // SOUND (traits, not names — an LLM does it, never string matching) and try+ // once more. This mirrors the video pipeline's "describe by traits" rule.+ if (!isContentBlock(error)) throw geminiApiError("Music generation failed.", error);+ const rewritten = await rewritePrompt(client, prompt);+ if (!rewritten) throw promptBlockedError();+ try {+ interaction = await client.interactions.create({ model, input: rewritten });+ } catch (retryError) {+ if (isContentBlock(retryError)) throw promptBlockedError();+ throw geminiApiError("Music generation failed.", retryError);+ }+ }++ // Usually already completed; poll as a safety net if the API ever returns+ // in_progress, so the caller still gets the finished clip in this one request.+ const start = Date.now();+ while (isRunning(interaction) && Date.now() - start < MAX_WAIT_MS) {+ await sleep(POLL_MS);+ try {+ interaction = await client.interactions.get(interaction.id);+ } catch (error) {+ throw geminiApiError("Polling the music interaction failed.", error);+ }+ }++ if (isRunning(interaction)) {+ throw new InferenceProviderError("Music generation is taking too long — try again.", {+ statusCode: 504,+ code: "music_generation_timeout",+ details: { model },+ });+ }++ return completedResult(interaction, generationId, model);+ }++ async function refreshAsset(+ generation: StoredGenerationForProvider,+ ): Promise<AssetGenerationProviderResult> {+ ensureConfigured(configured);++ const interactionId =+ generation.providerGenerationId?.trim() ||+ generation.providerJobId?.trim() ||+ stringValue(generation.metadata?.interactionId);+ if (!interactionId) {+ return failedResult(generation.model, "No music interaction to poll.");+ }++ const client = clientFactory(clientConfig.options);+ let interaction: Interaction;+ try {+ interaction = await client.interactions.get(interactionId);+ } catch (error) {+ throw geminiApiError("Polling the music interaction failed.", error);+ }++ if (isRunning(interaction)) {+ return {+ provider: providerID,+ model: generation.model,+ status: "in_progress",+ providerJobId: interactionId,+ providerGenerationId: interactionId,+ providerPollingUrl: interactionId,+ outputs: [],+ metadata: { provider: providerID, api: "interactions", interactionId },+ };+ }+ try {+ return completedResult(interaction, generation.id, generation.model);+ } catch (error) {+ if (error instanceof InferenceProviderError) {+ return failedResult(generation.model, error.message);+ }+ throw error;+ }+ }++ return {+ id: providerID,+ // Music only: advertising the generic "audio" capability would let this be a+ // fallback match for speech routing and misroute voiceovers here.+ capabilities: ["music"],+ configured,+ listModels,+ generateAsset,+ refreshAsset,+ };+}++function isRunning(interaction: Interaction): boolean {+ return interaction.status === "in_progress" || interaction.status === "requires_action";+}++// Lyria's policy rejection, by the provider's own error text (a technical field,+// not user input) — a named artist/song/brand, or other blocked content.+function isContentBlock(error: unknown): boolean {+ const message = error instanceof Error ? error.message : String(error);+ return /prohibited|sensitive word|content.?block|use policy|rephras|blocked|safety/i.test(message);+}++function promptBlockedError(): InferenceProviderError {+ return new InferenceProviderError(+ "Couldn't generate music for that description — describe the style, instruments, mood, and language instead of naming an artist or song.",+ { statusCode: 400, code: "music_prompt_blocked" },+ );+}++const REWRITE_INSTRUCTION =+ "You rewrite a music generation request so it passes a content policy that blocks " ++ "named artists, bands, songs, albums, and brands. Rewrite it to describe the SOUND " ++ "itself — genre, instrumentation, vocal style and gender, mood, energy, and tempo — " ++ "and keep the language the lyrics should be in and what they should be about. Never " ++ "name any real artist, band, song, album, or brand; translate any such reference into " ++ "its musical characteristics. Reply with ONLY the rewritten prompt, one or two vivid sentences.";++// Ask a fast model to restate a blocked prompt as a policy-safe description of the+// sound. Returns undefined on any failure, so the caller surfaces a clean block+// message rather than retrying with nothing.+async function rewritePrompt(+ client: GeminiMusicClient,+ prompt: string,+): Promise<string | undefined> {+ try {+ const response = await client.models.generateContent({+ model: geminiModels.flash,+ contents: [{ role: "user", parts: [{ text: `${REWRITE_INSTRUCTION}\n\nRequest: ${prompt}` }] }],+ });+ const text = (response as { text?: string }).text?.trim();+ return text && text !== prompt ? text : undefined;+ } catch {+ return undefined;+ }+}++// A completed interaction mapped to a provider result. Throws when the settled+// interaction carries no usable audio, so the route records a failed usage event+// instead of returning an empty success.+function completedResult(+ interaction: Interaction,+ generationId: string,+ model: string,+): AssetGenerationProviderResult {+ const audio = interaction.output_audio;+ if (interaction.status !== "completed" || !audio || (!audio.data && !audio.uri)) {+ throw new InferenceProviderError(+ interactionError(interaction) ?? "Gemini returned no music for this request.",+ {+ statusCode: 502,+ code: "empty_music_generation",+ details: { model, status: interaction.status ?? null },+ },+ );+ }+ const mimeType = audio.mime_type ?? "audio/mp3";+ const output: GenerationOutputRef = {+ id: `${generationId}-music-0`,+ kind: "audio",+ contentType: mimeType,+ filename: `${generationId}-0.${extensionForMime(mimeType)}`,+ metadata: { source: "provider-output" },+ ...(audio.data ? { dataBase64: audio.data } : { url: audio.uri }),+ };+ return {+ provider: providerID,+ model,+ status: "completed",+ // Billed flat per clip at completion (generationCount): a Lyria clip is a+ // fixed-length render, and the interaction carries no per-second usage.+ usage: { generationCount: 1 },+ outputs: [output],+ metadata: { provider: providerID, api: "interactions" },+ };+}++function failedResult(model: string, message: string): AssetGenerationProviderResult {+ return {+ provider: providerID,+ model,+ status: "failed",+ outputs: [],+ error: { message },+ metadata: { provider: providerID, api: "interactions" },+ };+}++function extensionForMime(mimeType: string): string {+ if (mimeType.includes("wav")) return "wav";+ if (mimeType.includes("ogg") || mimeType.includes("opus")) return "ogg";+ return "mp3";+}++// A failed interaction carries its reason on the last model_output step.+function interactionError(interaction: Interaction): string | undefined {+ for (const step of [...(interaction.steps ?? [])].reverse()) {+ const error = (step as unknown as JsonObject).error;+ if (isJsonObject(error)) {+ const message = stringValue(error.message);+ if (message) {+ return message;+ }+ }+ }+ return undefined;+}diff --git a/site/src/lib/inference/gemini-models.ts b/site/src/lib/inference/gemini-models.tsindex 2e44dbe6..f14c880a 100644--- a/site/src/lib/inference/gemini-models.ts+++ b/site/src/lib/inference/gemini-models.ts@@ -44,6 +44,18 @@ export const geminiTtsModels = {export type GeminiTtsModel = (typeof geminiTtsModels)[keyof typeof geminiTtsModels];+// Generative music (Gemini/Lyria) over the Interactions API: a text prompt in,+// an instrumental clip out — the background bed for a cut. `clip` renders a+// fixed ~30s clip; `pro` a full-length (~2min) track. These are the legacy+// Lyria interaction models, which run on our Vertex path (unlike Lyria RealTime,+// the live WebSocket API, which Vertex rejects). Bump here to adopt a newer Lyria.+export const geminiMusicModels = {+ clip: "lyria-3-clip-preview",+ pro: "lyria-3-pro-preview",+} as const;++export type GeminiMusicModel = (typeof geminiMusicModels)[keyof typeof geminiMusicModels];+// Semantic roles map a job to the model we run for it. Prefer referencing a// role over a bare constant so intent stays explicit at the call site.export const geminiModelRoles = {diff --git a/site/src/lib/inference/router.ts b/site/src/lib/inference/router.tsindex f93d2179..6138821b 100644--- a/site/src/lib/inference/router.ts+++ b/site/src/lib/inference/router.ts@@ -1,6 +1,6 @@-import { createAudioAssetProvider } from "@/lib/inference/adapters/audio-studio";import { createGeminiComputerUseProvider } from "@/lib/inference/adapters/gemini-computer-use";import { createGeminiImageAssetProvider } from "@/lib/inference/adapters/gemini-image";+import { createGeminiMusicAssetProvider } from "@/lib/inference/adapters/gemini-music";import { createGeminiSpeechAssetProvider } from "@/lib/inference/adapters/gemini-speech";import { createGeminiOmniVideoAssetProvider } from "@/lib/inference/adapters/gemini-omni-video";import { createHostedResponsesProvider } from "@/lib/inference/adapters/hosted-responses";@@ -225,10 +225,12 @@ export function createProviderRegistry() {createGeminiOmniVideoAssetProvider(),// Gemini TTS serves kind="speech" (voiceovers, subtitle read-alouds).createGeminiSpeechAssetProvider(),- // ElevenLabs serves kind="music" (the brief-to-video music bed). It gates- // itself on ELEVENLABS_API_KEY: with no key it reports unconfigured and is- // never selected, so kind="music" 503s and the pipeline assembles bedless.- createAudioAssetProvider(),+ // Gemini/Lyria is the sole kind="music" provider — the Audio-tab generator,+ // generate_music, and the brief-to-video bed. It runs on the always-hosted+ // Vertex service account, so music never falls back to another provider. The+ // adapter picks the clip or the longer pro model from the requested length so+ // a bed can still span a longer video (see gemini-music.ts).+ createGeminiMusicAssetProvider(),createGeminiComputerUseProvider(),createHostedResponsesProvider(),]);