Open source CapCut alternative.
TypeScript
refactor(site): refusal classes judged from the error
David Lec0090d90c5c9
diff --git a/site/src/cut/lib/genvideo/shotAttempts.ts b/site/src/cut/lib/genvideo/shotAttempts.tsindex d9ee6ed0..010b7268 100644--- a/site/src/cut/lib/genvideo/shotAttempts.ts+++ b/site/src/cut/lib/genvideo/shotAttempts.ts@@ -8,6 +8,7 @@* or a credit spent.*/+import { classifyProviderFailure } from "../providerFailure";import type { AssetRef } from "../assetRef";import type { VideoAttempt } from "../videoLadder";import type { VideoGenOptions } from "../generate";@@ -18,11 +19,9 @@ import type { RefAsset } from "./types";* itself — a safety policy on what the picture shows, an unreadable format —* rather than the render going wrong. Keyed on the provider's refusal, never* on what or who the content depicts. */-export function anchorRefused(err: string | null): boolean {- // "returned no video" is a completed render whose OUTPUT was filtered — for- // an image-anchored rung that's the safety policy refusing the content it- // rendered, the same dead end as refusing the anchor itself.- return err !== null && /input image|image format|person\/face|17301594|returned no video/i.test(err);+export async function anchorRefused(err: string | null): Promise<boolean> {+ if (err === null) return false;+ return (await classifyProviderFailure(err)) === "content_refused";}export interface ShotAttemptArgs {diff --git a/site/src/cut/lib/providerFailure.ts b/site/src/cut/lib/providerFailure.tsnew file mode 100644index 00000000..3fa708c7--- /dev/null+++ b/site/src/cut/lib/providerFailure.ts@@ -0,0 +1,48 @@+import { askJudge, choice } from "./judge";+import { hostedPost } from "./hosted";++// What a provider's failure means, judged from its own error text. A render+// or a music call fails for one of three reasons that call for different+// next steps: the provider declined the content (a policy or safety ground,+// an unreadable input image), something transient a retry could clear, or+// anything else. The error text is the provider's, never the user's words.++export type ProviderFailureClass = "content_refused" | "transient_failure" | "other";++export const PROVIDER_FAILURE_QUESTION = choice(+ "A hosted media provider answered a generation request with the error in `error`. Why did the request fail?",+ {+ content_refused:+ "The provider declined the content itself on a policy or safety ground: a blocked or sensitive prompt, a refused or unreadable input image (a face, a person, a format it cannot read), or a completed render whose output was filtered and returned nothing.",+ transient_failure: "A timeout, an overload, a quota or rate limit, or a network error that a retry could clear.",+ other: "Any other failure: a bad request, a missing parameter, an internal error with no policy or transient cause named.",+ },+);++/** The class a judgment cannot commit to. */+const CONFIDENCE_FLOOR = 0.5;++/** Classify a failure's cause from the judge's answer; below the confidence+ * floor it reads as "other". */+export function providerFailureClass(answer: {+ choice: ProviderFailureClass;+ confidence: number;+}): ProviderFailureClass {+ return answer.confidence >= CONFIDENCE_FLOOR ? answer.choice : "other";+}++/** The failure class of a provider error, judged through the hosted judge.+ * A judgment that cannot be made reads as "other", which is what a missed+ * pattern meant before. */+export async function classifyProviderFailure(error: string): Promise<ProviderFailureClass> {+ try {+ const { answers } = await askJudge(+ (payload, signal) => hostedPost("/api/inference/judge", payload, signal),+ { error: error.slice(0, 2000) },+ { cause: PROVIDER_FAILURE_QUESTION },+ );+ return providerFailureClass(answers.cause);+ } catch {+ return "other";+ }+}diff --git a/site/src/cut/lib/videoLadder.ts b/site/src/cut/lib/videoLadder.tsindex fa2a3075..7be71827 100644--- a/site/src/cut/lib/videoLadder.ts+++ b/site/src/cut/lib/videoLadder.ts@@ -18,7 +18,7 @@ export interface VideoAttempt {* reserve a rung for specific failures, e.g. text-only only after the* provider refused the image anchor. Skipped rungs cost nothing and keep* the prior error. */- gate?: (lastError: string | null) => boolean;+ gate?: (lastError: string | null) => boolean | Promise<boolean>;}/** Walk the rungs: run each in order, return on the first that lands. A rung@@ -36,7 +36,7 @@ export async function walkLadder(let lastError = "Video generation failed.";let anyFailed = false;for (const [rung, attempt] of attempts.entries()) {- if (attempt.gate && !attempt.gate(anyFailed ? lastError : null)) continue;+ if (attempt.gate && !(await attempt.gate(anyFailed ? lastError : null))) continue;try {await run(attempt, rung);return { ok: true, error: "", rung };diff --git a/site/src/lib/inference/adapters/gemini-music.ts b/site/src/lib/inference/adapters/gemini-music.tsindex 5055a836..759544f9 100644--- a/site/src/lib/inference/adapters/gemini-music.ts+++ b/site/src/lib/inference/adapters/gemini-music.ts@@ -1,3 +1,5 @@+import { typesafeJudge } from "@/lib/inference/adapters/typesafe";+import { PROVIDER_FAILURE_QUESTION, providerFailureClass } from "@/cut/lib/providerFailure";import { GoogleGenAI } from "@google/genai";import type { GoogleGenAIOptions, Interactions } from "@google/genai";@@ -149,13 +151,13 @@ export function createGeminiMusicAssetProvider(// 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);+ if (!(await 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();+ if (await isContentBlock(retryError)) throw promptBlockedError();throw geminiApiError("Music generation failed.", retryError);}}@@ -244,11 +246,20 @@ 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 {+// Lyria's policy rejection — a named artist/song/brand, or other blocked+// content — judged from the provider's own error text. A failure the judge+// cannot classify reads as an ordinary error.+async function isContentBlock(error: unknown): Promise<boolean> {const message = error instanceof Error ? error.message : String(error);- return /prohibited|sensitive word|content.?block|use policy|rephras|blocked|safety/i.test(message);+ try {+ const { answers } = await typesafeJudge().askJudge({+ state: { error: message.slice(0, 2000) },+ questions: { cause: PROVIDER_FAILURE_QUESTION },+ });+ return providerFailureClass(answers.cause) === "content_refused";+ } catch {+ return false;+ }}function promptBlockedError(): InferenceProviderError {