Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Hosted runs: price and set aside a run for the outputs it will make

Adarsh3b2a4df81b87
diff --git a/src/app/api/generations/route.ts b/src/app/api/generations/route.ts
index 8989645a..9cb81aad 100644
--- a/src/app/api/generations/route.ts
+++ b/src/app/api/generations/route.ts
@@ -140,7 +140,7 @@ export async function POST(request: Request) {
decision = await decidePlace(account, req, wish);
if (decision.kind === "refuse") return refusedBy(decision.refusal);
}
- const onPool = decision.kind === "pool" ? decision : null;
+ let onPool = decision.kind === "pool" ? decision : null;
if (account && onPool && billingConfigured()) {
// The billing service's limits for runs on this install's GPUs (never above the studio's own).
const lim = onPool.policy.limits;
@@ -151,6 +151,9 @@ export async function POST(request: Request) {
if (lim.outputsPerRun > 0 && (req.params?.numOutputs ?? 1) > lim.outputsPerRun) {
req = { ...req, params: { ...req.params, numOutputs: lim.outputsPerRun } };
notes.push(ACCOUNT_MESSAGES.outputsCapped);
+ // Priced and set aside for what runs.
+ const capped = req;
+ onPool = { ...onPool, planned: runOnPool(account.id, () => previewGeneration(capped)) };
}
}
// GPU pre-flight: a ComfyUI run that is sure to fail for something missing stops here with the plan that fixes it
diff --git a/src/server/hosted-run.ts b/src/server/hosted-run.ts
index bda22ca6..4b52b95b 100644
--- a/src/server/hosted-run.ts
+++ b/src/server/hosted-run.ts
@@ -25,6 +25,7 @@ import { hostedRunsOn } from "./hosted-switch";
import { checkRun, moderationConfigured } from "./moderation";
import type { RunStamp } from "./run-stamp";
import { readRef } from "./storage";
+import { ACCOUNT_LIMITS } from "./account-files";
import { runAsTenant } from "./tenant";
/**
@@ -184,8 +185,12 @@ export async function reservePool(account: Account, req: GenerationRequest, plan
* A price check on this install's GPUs (hosted runs on): the plan there and the billing service's quote (no service:
* free, and Run keeps its own words). Null when the run wouldn't go there (its own GPU, its own key, today's answer).
*/
-export async function poolQuote(account: Account | null, req: GenerationRequest, wish: PlaceWish): Promise<{ planned: Planned; quote: Quote } | null> {
+export async function poolQuote(account: Account | null, asked: GenerationRequest, wish: PlaceWish): Promise<{ planned: Planned; quote: Quote } | null> {
if (!hostedRunsOn() || wish === "own" || !poolReady()) return null;
+ const p = account ? await billingPolicy(account.id) : null;
+ // Priced for the outputs it would make: the studio's cap per run, and the billing service's when lower.
+ const cap = Math.min(ACCOUNT_LIMITS.outputsPerRun, ...(billingConfigured() && p && p.limits.outputsPerRun > 0 ? [p.limits.outputsPerRun] : []));
+ const req = (asked.params?.numOutputs ?? 1) > cap ? { ...asked, params: { ...asked.params, numOutputs: cap } } : asked;
let planned: Planned;
try {
planned = runOnPool(account?.id ?? null, () => previewGeneration(req));
@@ -194,10 +199,7 @@ export async function poolQuote(account: Account | null, req: GenerationRequest,
}
if (planned.route.provider !== "comfyui") return null;
if (account && wish !== "pool" && accountGpu(account.id) && (await ownGpuOnline(account.id))) return null;
- if (account) {
- const p = await billingPolicy(account.id);
- if (p && (!p.hosted || !poolAllows(p, planned.model.id, req.presetId))) return null;
- }
+ if (p && (!p.hosted || !poolAllows(p, planned.model.id, req.presetId))) return null;
const kind = outputKind(planned.op, planned.model);
const q = await billingQuote(account ? accountFacts(account) : null, runFacts(req, planned, kind), queueFacts(account?.id ?? null));
return { planned, quote: q };
diff --git a/tests/billing-hook.test.ts b/tests/billing-hook.test.ts
index d94189f7..091adec0 100644
--- a/tests/billing-hook.test.ts
+++ b/tests/billing-hook.test.ts
@@ -161,6 +161,16 @@ describe("the billing hook with a service", () => {
expect(items.every((g) => statusOf(g.id).status === "succeeded")).toBe(true);
});
+ it("prices and sets aside only what runs: the service's outputs-per-run limit applies before the quote and the reserve", async () => {
+ const p = await post(run({ params: { numOutputs: 4 } }), cookie, "?preview=1");
+ expect(((await p.json()) as { outputs: number }).outputs).toBe(2);
+ expect((fake.calls_of("/v1/quote").at(-1)!.body as { run: { jobs: number } }).run.jobs).toBe(2);
+ const r = await post({ ...run({ params: { numOutputs: 4 } }), shown: { amount: 7, priceVersion: "v7" } });
+ expect(r.status).toBe(201);
+ expect(((await r.json()) as { items: unknown[] }).items).toHaveLength(2);
+ expect((fake.calls_of("/v1/reserve").at(-1)!.body as { run: { jobs: number } }).run.jobs).toBe(2);
+ });
+
it("never takes the hold, the account or the place's stamp from the client", async () => {
const other = auth.createAccount("[email protected]", "long enough").id;
const r = await post({ ...run(), hold: "h_evil", account: other, charge: "free", checkOutputs: false, waitMs: 1, shown: { amount: 7, priceVersion: "v7" } });