Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): the asset preflight counts before it reads storage

David Le6039d0dbb357
diff --git a/site/src/app/api/inference/assets/route.ts b/site/src/app/api/inference/assets/route.ts
index 07634631..ced01e2b 100644
--- a/site/src/app/api/inference/assets/route.ts
+++ b/site/src/app/api/inference/assets/route.ts
@@ -50,10 +50,12 @@ export const POST = withDonkeyAuth(async (request) => {
// length), so asking the provider is the only way the preflight sees the id that gets charged.
// Reference pictures and seed frames ride storage, not the request body — they become inline
// data on the server-to-server leg. That resolution reads whole objects out of storage, so it
- // waits for the balance to clear; only a provider whose charge scales with the input (the matte
- // segmenter's frame chunks) resolves ahead of the preflight, because its count is read from the
- // resolved media.
- const resolveRequest = async () => {
+ // waits for the balance to clear; a provider whose count is read from the resolved media (the
+ // matte segmenter's frame chunks) pulls it ahead of the preflight through the resolver it is
+ // handed. The promise is memoized, so the request resolves at most once either way.
+ let resolving: Promise<typeof parsed.data> | undefined;
+ const resolveRequest = () => (resolving ??= resolveRequestOnce());
+ const resolveRequestOnce = async () => {
const inputs: JsonObject | undefined = parsed.data.inputs
? toJsonObject(
await resolveInferenceBlobs(
@@ -73,8 +75,7 @@ export const POST = withDonkeyAuth(async (request) => {
model = provider.assetModelFor?.(parsed.data) ?? parsed.data.model;
generationRequest = parsed.data;
if (provider.assetGenerationCountFor) {
- generationRequest = await resolveRequest();
- generationCount = await provider.assetGenerationCountFor(generationRequest);
+ generationCount = await provider.assetGenerationCountFor(parsed.data, resolveRequest);
}
} catch (error) {
// A rejected request still leaves its diagnostic trail, same as a
@@ -116,7 +117,7 @@ export const POST = withDonkeyAuth(async (request) => {
const failedUsageProvider = provider.id;
try {
- if (!provider.assetGenerationCountFor) generationRequest = await resolveRequest();
+ generationRequest = await resolveRequest();
const generationId = generationIDForRequest(parsed.data);
const generation = {
id: generationId,
diff --git a/site/src/lib/inference/adapters/fal-matte.ts b/site/src/lib/inference/adapters/fal-matte.ts
index f0adc5bd..d5cc1363 100644
--- a/site/src/lib/inference/adapters/fal-matte.ts
+++ b/site/src/lib/inference/adapters/fal-matte.ts
@@ -160,9 +160,11 @@ export function createFalMatteProvider(
}
// The route's pre-preflight count: the chunks this segment will bill, read
- // from the resolved segment itself in the resolved model's own unit.
+ // from the resolved segment itself in the resolved model's own unit. The
+ // count needs the media, so this is the provider that pulls the resolver.
async function assetGenerationCountFor(
request: AssetGenerationProviderRequest["request"],
+ resolveInputs: () => Promise<AssetGenerationProviderRequest["request"]>,
): Promise<number> {
ensureConfigured(configured);
if (request.kind !== "matte") {
@@ -171,7 +173,8 @@ export function createFalMatteProvider(
code: "unsupported_asset_kind",
});
}
- return billableChunks(segmentUrl(request), matteChunkFrames[resolveModel(request.model)]);
+ const resolved = await resolveInputs();
+ return billableChunks(segmentUrl(resolved), matteChunkFrames[resolveModel(resolved.model)]);
}
async function generateAsset({
diff --git a/site/src/lib/inference/providers.ts b/site/src/lib/inference/providers.ts
index 8ee006ba..0c3c5018 100644
--- a/site/src/lib/inference/providers.ts
+++ b/site/src/lib/inference/providers.ts
@@ -145,12 +145,18 @@ export type InferenceProvider = {
// which is what lets the preflight price the generation. Throws the same way generateAsset would
// for an unpriced model, only sooner.
assetModelFor?: (request: AssetGenerationRequest) => string;
- // How many generation units this request will bill, for a provider whose
- // charge scales with the input (the matte segmenter's frame chunks). The
- // route calls it with resolved inputs before the credit preflight, so the
- // balance check covers the whole charge, and hands the count back through
- // generateAsset's generationCount.
- assetGenerationCountFor?: (request: AssetGenerationRequest) => Promise<number>;
+ // How many generation units this request will bill. The route calls it
+ // before the credit preflight, so the balance check covers the whole charge,
+ // and hands the count back through generateAsset's generationCount. Reading
+ // the inputs costs whole objects out of storage, so the request arrives
+ // unresolved and `resolveInputs` fetches them: a provider counting off the
+ // media (the matte segmenter's frame chunks) awaits it, one counting off the
+ // parameters alone (the video renderer's seconds) never does, and an empty
+ // balance is refused before anything leaves storage.
+ assetGenerationCountFor?: (
+ request: AssetGenerationRequest,
+ resolveInputs: () => Promise<AssetGenerationRequest>,
+ ) => Promise<number>;
generateAsset?: (
request: AssetGenerationProviderRequest,
) => Promise<AssetGenerationProviderResult>;