Open source CapCut alternative.
TypeScript
fix(site): per-user pacing, dev bypass as a server switch, card route misses
David Lebce70c044bdf
diff --git a/site/.env.example b/site/.env.exampleindex a11b8a11..10668667 100644--- a/site/.env.example+++ b/site/.env.example@@ -9,9 +9,6 @@ CRON_SECRET="replace-with-a-long-random-secret"GOOGLE_CLIENT_ID="replace-with-google-web-client-id"GOOGLE_CLIENT_SECRET="replace-with-google-web-client-secret"-# Required for macOS desktop computer-use through OpenAI.-OPENAI_API_KEY="replace-with-openai-api-key"-# Optional hosted inference providers.ELEVENLABS_API_KEY="replace-with-elevenlabs-api-key"@@ -22,10 +19,13 @@ FAL_KEY="replace-with-fal-api-key"# uses the global endpoint and derives the project from project_id.GOOGLE_APPLICATION_CREDENTIALS_JSON='{"type":"service_account",...}'-# Dev-only: a Gemini Developer API key. When set, the Live client connects-# directly to the Developer API (using GEMINI_LIVE_MODEL; the client default is an-# audio-output-only model) instead of minting a Vertex token. Production uses the-# keyless Vertex path above.+# Dev-only: lets the eval scripts and a hand-run worker call the dev server+# with the x-donkey-dev-auth-bypass header (no session, no credits). Only read+# when NODE_ENV is not production; a hosted deployment never sets it.+DONKEY_DEV_AUTH_BYPASS=1++# Dev-only: a Gemini Developer API key. Production uses the keyless Vertex+# path above.GEMINI_API_KEY="replace-with-gemini-api-key"# Stripe. The Pro subscription plus one-time credit top-ups run through@@ -39,10 +39,6 @@ STRIPE_WEBHOOK_SECRET="replace-with-stripe-webhook-signing-secret"STRIPE_PRO_PRICE_ID="replace-with-stripe-pro-price-id"STRIPE_PORTAL_CONFIGURATION_ID="replace-with-stripe-portal-configuration-id"-# Browser Use Cloud (agentic web automation via /api/browser/run). Backend-only-# secret; the Mac app never carries it. Key starts with "bu_".-BROWSER_USE_API_KEY="replace-with-browser-use-api-key"-# Resend (transactional email + contact sync). Unset in dev skips email work:# signups log the skip and provision everything else. The from address is code# (src/lib/email/resend.ts). RESEND_WEBHOOK_SECRET signs the contact.updateddiff --git a/site/scripts/lib/cut-eval/README.md b/site/scripts/lib/cut-eval/README.mdindex 0172dfae..bae543b6 100644--- a/site/scripts/lib/cut-eval/README.md+++ b/site/scripts/lib/cut-eval/README.md@@ -9,7 +9,8 @@ npm run eval:cut-chat -- [--runs N] [--only <case>] [--bucket chat|single-tool|m```It replays real composer turns against the live chat model through the dev-server's hosted Responses route. Start `next dev` on :3000 first. Auth is the+server's hosted Responses route. Start `next dev` on :3000 first, with+`DONKEY_DEV_AUTH_BYPASS=1` in its env (`.env.example` carries it). Auth is thedev bypass header, so runs spend no credits. The spoken fixture is synthesizedwith macOS `say`, so runs need a Mac.diff --git a/site/src/app/api/inference/assets/refresh/route.ts b/site/src/app/api/inference/assets/refresh/route.tsindex dd6e5769..67bb9683 100644--- a/site/src/app/api/inference/assets/refresh/route.ts+++ b/site/src/app/api/inference/assets/refresh/route.ts@@ -13,7 +13,10 @@ import {validationErrorResponse,} from "@/lib/inference/responses";import { storedGenerationForProviderSchema } from "@/lib/inference/schemas";-import { withDonkeyAuth } from "@/lib/donkey-api-auth";+import {+ INFERENCE_RATE_LIMIT,+ withDonkeyAuth,+} from "@/lib/donkey-api-auth";import { InferenceProviderError } from "@/lib/inference/providers";export const POST = withDonkeyAuth(async (request) => {@@ -82,4 +85,4 @@ export const POST = withDonkeyAuth(async (request) => {throw error;}-}, { allowRunner: true });+}, { allowRunner: true, rateLimit: INFERENCE_RATE_LIMIT });diff --git a/site/src/app/api/inference/assets/route.ts b/site/src/app/api/inference/assets/route.tsindex 3cd21a47..07634631 100644--- a/site/src/app/api/inference/assets/route.ts+++ b/site/src/app/api/inference/assets/route.ts@@ -20,7 +20,10 @@ import {validationErrorResponse,} from "@/lib/inference/responses";import { assetGenerationRequestSchema } from "@/lib/inference/schemas";-import { withDonkeyAuth } from "@/lib/donkey-api-auth";+import {+ INFERENCE_RATE_LIMIT,+ withDonkeyAuth,+} from "@/lib/donkey-api-auth";import { resolveInferenceBlobs } from "@/lib/inference/blobs";import { InferenceProviderError, type JsonObject } from "@/lib/inference/providers";import { toJsonObject, toJsonValue } from "@/lib/inference/json";@@ -223,4 +226,4 @@ export const POST = withDonkeyAuth(async (request) => {throw error;}-}, { allowRunner: true });+}, { allowRunner: true, rateLimit: INFERENCE_RATE_LIMIT });diff --git a/site/src/app/api/inference/models/route.ts b/site/src/app/api/inference/models/route.tsindex 375c26bb..b1039d07 100644--- a/site/src/app/api/inference/models/route.ts+++ b/site/src/app/api/inference/models/route.ts@@ -3,7 +3,10 @@ import { NextResponse } from "next/server";import { createProviderRegistry } from "@/lib/inference/router";import { requireInferenceClientId } from "@/lib/inference/responses";import { parseRequestedModalities } from "@/lib/inference/schemas";-import { withDonkeyAuth } from "@/lib/donkey-api-auth";+import {+ INFERENCE_RATE_LIMIT,+ withDonkeyAuth,+} from "@/lib/donkey-api-auth";export const GET = withDonkeyAuth(async (request) => {const client = requireInferenceClientId(request.donkey.clientId);@@ -20,4 +23,4 @@ export const GET = withDonkeyAuth(async (request) => {return NextResponse.json({data: models,});-});+}, { rateLimit: INFERENCE_RATE_LIMIT });diff --git a/site/src/app/api/inference/responses/route.ts b/site/src/app/api/inference/responses/route.tsindex e95623d7..5d918741 100644--- a/site/src/app/api/inference/responses/route.ts+++ b/site/src/app/api/inference/responses/route.ts@@ -22,6 +22,7 @@ import {import { InferenceProviderError } from "@/lib/inference/providers";import { responseCreateRequestSchema } from "@/lib/inference/schemas";import {+ INFERENCE_RATE_LIMIT,shouldBypassDonkeyInferenceCredits,withDonkeyAuth,} from "@/lib/donkey-api-auth";@@ -244,4 +245,4 @@ export const POST = withDonkeyAuth(async (request) => {throw error;}-}, { allowRunner: true });+}, { allowRunner: true, rateLimit: INFERENCE_RATE_LIMIT });diff --git a/site/src/app/api/inference/uploads/route.ts b/site/src/app/api/inference/uploads/route.tsindex 9d98447c..a2ab7d78 100644--- a/site/src/app/api/inference/uploads/route.ts+++ b/site/src/app/api/inference/uploads/route.ts@@ -11,7 +11,10 @@ import {validationErrorResponse,} from "@/lib/inference/responses";import { inferenceUploadRequestSchema } from "@/lib/inference/schemas";-import { withDonkeyAuth } from "@/lib/donkey-api-auth";+import {+ INFERENCE_RATE_LIMIT,+ withDonkeyAuth,+} from "@/lib/donkey-api-auth";// Media a hosted inference call carries rides object storage rather than the// request body (see lib/inference/blobs.ts). This mints the upload URLs: one@@ -80,4 +83,4 @@ export const POST = withDonkeyAuth(async (request) => {}throw error;}-}, { allowRunner: true });+}, { allowRunner: true, rateLimit: INFERENCE_RATE_LIMIT });diff --git a/site/src/app/cut/s/[token]/card/[ext]/route.ts b/site/src/app/cut/s/[token]/card/[ext]/route.tsindex af8a0575..2d258da1 100644--- a/site/src/app/cut/s/[token]/card/[ext]/route.ts+++ b/site/src/app/cut/s/[token]/card/[ext]/route.ts@@ -23,6 +23,9 @@ export async function GET({ params }: { params: Promise<{ token: string; ext: string }> }) {const { token, ext } = await params;+ // Two card kinds exist. Anything else is a miss, answered before any+ // lookup or render is spent on it.+ if (ext !== "gif" && ext !== "jpg") return new Response("Not found.", { status: 404 });const view = await resolveShare(token, request);if (view instanceof Response) return view;const target = { userId: view.share.userId, projectId: view.share.projectId };@@ -33,10 +36,8 @@ export async function GET(}: { "Cache-Control": "private, no-store" };- if (ext === "gif" || ext === "jpg") {- const url = await cardUrl(target, ext as CardKind);- if (url) return new Response(null, { status: 302, headers: { ...headers, Location: url } });- }+ const url = await cardUrl(target, ext as CardKind);+ if (url) return new Response(null, { status: 302, headers: { ...headers, Location: url } });// No card yet — draw one so the link never unfurls blank. Kept on a short// leash so the rendered card takes over as soon as it exists.diff --git a/site/src/lib/donkey-api-auth.ts b/site/src/lib/donkey-api-auth.tsindex 53b3d479..a60106c4 100644--- a/site/src/lib/donkey-api-auth.ts+++ b/site/src/lib/donkey-api-auth.ts@@ -1,3 +1,4 @@+import { checkInMemoryRateLimit, rateLimitResponse } from "@/lib/inference/rate-limit";import { createHash, timingSafeEqual } from "node:crypto";import { type NextRequest, NextResponse } from "next/server";@@ -26,6 +27,10 @@ export type DonkeyAuthOptions = {// Accept a `dk_live_` bearer key as the user. Off by default: only routes// that opt in (the Cut API surface) take machine callers.allowApiKey?: boolean;+ // Requests one user may make through this handler per window. The default+ // fits an editor polling several jobs at once; routes that run a model per+ // call set a tighter one.+ rateLimit?: { limit: number; windowMs: number };// Accept the Cut runner's shared-secret grant. Off by default: only the// routes the worker actually calls back (the Cut API surface and hosted// inference) opt in, so a leaked secret stays contained to them.@@ -79,6 +84,9 @@ export type DonkeyAuthHandler<const clientIdHeader = "x-donkey-client-id";const conversationIdHeader = "x-donkey-conversation-id";const devAuthBypassHeader = "x-donkey-dev-auth-bypass";+const DEFAULT_RATE_LIMIT = { limit: 900, windowMs: 60_000 };+/** For handlers that run a model per request. */+export const INFERENCE_RATE_LIMIT = { limit: 300, windowMs: 60_000 };const devAuthBypassUserID = "donkey-dev-auth-bypass";function conversationIdFromHeaders(headers: Headers): string | null {@@ -197,10 +205,16 @@ function runnerAuthContext(headers: Headers): DonkeyAuthContext | null {};}+// The bypass is a dev-server switch: it has to be turned on in the server's+// own environment as well as asked for by the caller, so a build that merely+// forgot NODE_ENV never answers the header. Production refuses it outright.function devAuthBypassContext(headers: Headers): DonkeyAuthContext | null {if (process.env.NODE_ENV === "production") {return null;}+ if (process.env.DONKEY_DEV_AUTH_BYPASS !== "1") {+ return null;+ }if (headers.get(devAuthBypassHeader)?.trim() !== "1") {return null;}@@ -246,6 +260,19 @@ export function withDonkeyAuth<return forbiddenResponse();}+ // Every authenticated handler is paced per user. Buckets live in this+ // process, so the ceiling is per instance; it still turns a hot loop+ // against one route into 429s instead of a bill.+ const pace = options.rateLimit ?? DEFAULT_RATE_LIMIT;+ const limited = checkInMemoryRateLimit({+ key: `${authContext.userId}:${pace.limit}:${pace.windowMs}`,+ limit: pace.limit,+ windowMs: pace.windowMs,+ });+ if (!limited.ok) {+ return rateLimitResponse(limited.retryAfterSeconds);+ }+const authenticatedRequest = Object.assign(request, {donkey: authContext,}) as TReq;diff --git a/site/src/lib/inference/rate-limit.ts b/site/src/lib/inference/rate-limit.tsindex 37a93433..b6a13d98 100644--- a/site/src/lib/inference/rate-limit.ts+++ b/site/src/lib/inference/rate-limit.ts@@ -55,7 +55,7 @@ export function rateLimitResponse(retryAfterSeconds: number) {return NextResponse.json({error: "rate_limited",- message: "Too many screenshot parse requests. Please retry shortly.",+ message: "Too many requests. Please retry shortly.",},{headers: {