Skip to content

Open source CapCut alternative.

TypeScript

fix(site): ceilings on the hosted caption, title, and transcript helpers

David Lebf1311ed7bb4
diff --git a/site/src/cut/server/cloud/captions.ts b/site/src/cut/server/cloud/captions.ts
index 9cc7af26..71a51ba9 100644
--- a/site/src/cut/server/cloud/captions.ts
+++ b/site/src/cut/server/cloud/captions.ts
@@ -39,6 +39,10 @@ const MODEL = geminiModelRoles.chat;
const MAX_FRAMES = 24;
const MAX_BODY_BYTES = 4 * 1024 * 1024; // stay inside the hosted body limit
+// One credit check covers the whole run, and the run is one model call per
+// batch of cues, so the cue count is what bounds the spend behind that check.
+// Transcripts run to about thirty cues a minute; this holds a three-hour lane.
+const MAX_CUES = 6000;
type GeminiPart = { text: string } | { inlineData: { mimeType: string; data: string } };
@@ -173,7 +177,9 @@ export const captionsCloud = {
let style = "clean";
let translateTo = "";
try {
- const body = (await req.json()) as {
+ const text = await req.text();
+ if (text.length > MAX_BODY_BYTES) return err("Too many cues.", 413);
+ const body = JSON.parse(text) as {
cues?: CaptionInput[];
style?: string;
translateTo?: string;
@@ -181,6 +187,7 @@ export const captionsCloud = {
cues = Array.isArray(body.cues)
? body.cues.filter((c) => c && typeof c.text === "string")
: [];
+ if (cues.length > MAX_CUES) return err("Too many cues.", 413);
if (typeof body.style === "string") style = body.style;
if (typeof body.translateTo === "string") translateTo = body.translateTo;
} catch {
diff --git a/site/src/cut/server/cloud/clipTitle.ts b/site/src/cut/server/cloud/clipTitle.ts
index 8bd4029c..f6916e53 100644
--- a/site/src/cut/server/cloud/clipTitle.ts
+++ b/site/src/cut/server/cloud/clipTitle.ts
@@ -26,6 +26,7 @@ import { geminiModelRoles } from "@/lib/inference/gemini-models";
import { toJsonValue } from "@/lib/inference/json";
import type { JsonValue } from "@/lib/inference/providers";
import { prisma } from "@/lib/prisma";
+import { cutLimitsFor } from "./limits";
import { err } from "./util";
const ROUTE = "/api/cut-cloud/library/title";
@@ -41,6 +42,23 @@ const FRAME_TYPES = new Set(["image/jpeg", "image/png", "image/webp"]);
/** Long enough that a hovering card can hand the whole line over, short enough
* that the tile reads at a glance. */
const MAX_TITLE_CHARS = 60;
+/** Whether one more title fits today's allowance — titles are on the house,
+ * so the tier sets a daily ceiling, and past it the tile shows the file name
+ * until tomorrow. Counted from the route's own usage events so the ledger is
+ * the tracker. */
+async function withinTitleAllowance(userId: string): Promise<boolean> {
+ const { titlesPerDay } = await cutLimitsFor(userId);
+ if (titlesPerDay === null) return true;
+ const used = await prisma.inferenceUsageEvent.count({
+ where: {
+ userId,
+ route: ROUTE,
+ status: "succeeded",
+ createdAt: { gte: new Date(Date.now() - 24 * 60 * 60 * 1000) },
+ },
+ });
+ return used < titlesPerDay;
+}
type GeminiPart = { text: string } | { inlineData: { mimeType: string; data: string } };
@@ -138,6 +156,7 @@ export const clipTitleCloud = {
// shelf, a listing that arrived before the write — gets the title back
// without spending a model call on it.
if (meta.title) return Response.json({ title: meta.title });
+ if (!(await withinTitleAllowance(userId))) return err("Titling is paused for today.", 429);
const clientConfig = geminiClientConfig(process.env);
if (!clientConfig.configured) return err("Titling is not configured on this deployment.", 500);
diff --git a/site/src/cut/server/cloud/transcribe.ts b/site/src/cut/server/cloud/transcribe.ts
index 0d594030..4da10f4a 100644
--- a/site/src/cut/server/cloud/transcribe.ts
+++ b/site/src/cut/server/cloud/transcribe.ts
@@ -27,9 +27,12 @@ import { toJsonValue } from "@/lib/inference/json";
import type { JsonValue } from "@/lib/inference/providers";
import { err } from "./util";
-// The client sends sub-megabyte chunks of 16-bit mono PCM; anything bigger is
-// not ours and would blow past inline-audio comfort anyway.
-const MAX_AUDIO_BYTES = 4 * 1024 * 1024;
+// The client sends 16 kHz 16-bit mono PCM in chunks of at most 20 seconds; the
+// allowance below counts chunks, so the chunk itself is held to that length
+// (with a little room), or an oversized chunk would be minutes on the house.
+const PCM_BYTES_PER_SECOND = 32_000;
+const MAX_CHUNK_SECONDS = 30;
+const MAX_AUDIO_BYTES = 44 + MAX_CHUNK_SECONDS * PCM_BYTES_PER_SECOND;
const ROUTE = "/api/cut-cloud/transcribe/";
const PROVIDER = "gemini";
// The registry's chat flash: multimodal, takes inline audio, priced in
@@ -188,7 +191,7 @@ export const transcribeCloud = {
// Seconds of 16 kHz s16 mono after the 44-byte RIFF header — the clamp
// ceiling for cue times the model may overshoot.
- const audioSeconds = Math.max(1, (audio.size - 44) / 32000);
+ const audioSeconds = Math.max(1, (audio.size - 44) / PCM_BYTES_PER_SECOND);
const wavBase64 = Buffer.from(await audio.arrayBuffer()).toString("base64");
let raw: unknown;