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: stop a run on this install's GPUs there, tell an optional billing service about sign-ups, sign-ins and deletions

Adarsh6d7d07854ff6
diff --git a/src/app/api/auth/login/route.ts b/src/app/api/auth/login/route.ts
index 3268fd42..0542f05d 100644
--- a/src/app/api/auth/login/route.ts
+++ b/src/app/api/auth/login/route.ts
@@ -1,5 +1,6 @@
import { cookies } from "next/headers";
import { z } from "zod";
+import { accountEvent, accountFacts } from "@/server/billing";
import { authEnabled, createSession, LoginGuard, normalizeEmail, publicSignup, recordLogin, SESSION_COOKIE, verifyAccount, verifyPassword } from "@/server/auth";
import { enter, hit } from "@/server/rate";
@@ -96,6 +97,7 @@ export async function POST(request: Request) {
if (account.disabled) return Response.json({ error: "This account is switched off. Ask the studio's owner." }, { status: 403 });
accountId = account.id;
recordLogin(account.id);
+ accountEvent("signed-in", accountFacts(account));
} else {
const wait = ownerGuard().blockedFor(key);
if (wait > 0) return tooMany(wait, key);
diff --git a/src/app/api/auth/signup/route.ts b/src/app/api/auth/signup/route.ts
index da6ef0bf..5dd3a9aa 100644
--- a/src/app/api/auth/signup/route.ts
+++ b/src/app/api/auth/signup/route.ts
@@ -1,5 +1,6 @@
import { cookies } from "next/headers";
import { z } from "zod";
+import { accountEvent, accountFacts } from "@/server/billing";
import { AccountError, createAccount, createSession, LoginGuard, publicSignup, SESSION_COOKIE } from "@/server/auth";
import { hit, peek } from "@/server/rate";
@@ -43,6 +44,8 @@ export async function POST(request: Request) {
throw e;
}
if (key !== LoginGuard.DIRECT) hit(`signup-day:${key}`, SIGNUPS_A_DAY, DAY);
+ // An optional billing service hears of it (none set: nothing is sent).
+ accountEvent("created", accountFacts(account));
const s = createSession(account.id);
(await cookies()).set(SESSION_COOKIE, s.value, {
httpOnly: true,
diff --git a/src/server/account-sweep.ts b/src/server/account-sweep.ts
index a3daa5b1..e7a1418f 100644
--- a/src/server/account-sweep.ts
+++ b/src/server/account-sweep.ts
@@ -3,7 +3,8 @@ import fs from "node:fs";
import path from "node:path";
import { RETENTION_MS, runAccountDeletedHooks } from "./account-files";
import { clearAccountLoras } from "./account-loras";
-import { deleteAccount, readAccountIds, setAccountDisabled } from "./auth";
+import { accountById, deleteAccount, readAccountIds, setAccountDisabled } from "./auth";
+import { accountEvent, accountFacts } from "./billing";
import { db } from "./db";
import { handOversDone } from "./generations";
import { deleteForever } from "./library";
@@ -60,7 +61,10 @@ export async function removeAccount(id: string): Promise<void> {
.all(id) as string[];
await purgeAccount(id);
if (!(await handOversDone(going))) console.warn("[fuobe] account deletion: a run was still being handed to its provider after a minute; the account goes anyway");
+ const gone = accountById(id);
deleteAccount(id);
+ // An optional billing service hears of it (none set: nothing is sent).
+ if (gone) accountEvent("deleted", accountFacts(gone));
}
/** One sweep (see the header). `now` for tests. */
diff --git a/src/server/generations.ts b/src/server/generations.ts
index 20ec92f5..70ba6d7b 100644
--- a/src/server/generations.ts
+++ b/src/server/generations.ts
@@ -415,6 +415,8 @@ export const ASSET_FREE: Record<string, string> = {
"gpu_snapshots.inventory_json": "what a GPU's ComfyUI has (class names, model file names and sizes), never a Fuobe file",
"gpu_snapshots.report_json": "readiness built from that inventory",
"generations.external_meta_json": "the provider's job handles",
+ "billing_outbox.payload_json": "reports for the billing service: hold and run ids, counts and words, never a file",
+ "reports_inbox.target_json": "what a report is about: a run, asset or share id, never a file",
"shorts_sessions.range_json": "start and end times",
"shorts_sessions.settings_json": "ratio, language, model and resolution",
"shorts_sessions.segments_json": "generation ids of each take",
@@ -2344,11 +2346,14 @@ export async function cancelGeneration(id: string): Promise<void> {
const account = accountOf(r);
// An account's job is stopped with its own key, also when the account is switched off or about to be deleted.
const own = account ? accountRun(account, r.provider, "cancel") : undefined;
+ // A run on this install's GPUs is stopped there, as the account.
+ const onPool = !!account && poolOf(r);
const cancel = () =>
Promise.resolve()
.then(() => adapterFor(r.provider, account).cancel?.(pollContext(r, model)))
.catch(() => {});
- if (!own) await cancel();
+ if (onPool) await runOnPool(account!, cancel);
+ else if (!own) await cancel();
else if ("run" in own) await own.run(cancel);
}
stopModelFinish(id);
diff --git a/tests/billing-hook.test.ts b/tests/billing-hook.test.ts
index 5855ebfc..d94189f7 100644
--- a/tests/billing-hook.test.ts
+++ b/tests/billing-hook.test.ts
@@ -314,6 +314,17 @@ describe("the outbox", () => {
});
});
+describe("account events", () => {
+ it("a deleted account is reported to the service", async () => {
+ const { removeAccount } = await import("@/server/account-sweep");
+ const gone = auth.createAccount("[email protected]", "long enough").id;
+ await removeAccount(gone);
+ await until(() => fake.calls_of("/v1/account-event").some((c) => (c.body as { event: string; account: { id: string } }).account.id === gone));
+ const ev = fake.calls_of("/v1/account-event").find((c) => (c.body as { account: { id: string } }).account.id === gone)!.body as { event: string; account: { email: string } };
+ expect(ev).toMatchObject({ event: "deleted", account: { email: "[email protected]" } });
+ });
+});
+
describe("the transport", () => {
it("reads https, http and unix: addresses, and nothing else", () => {
expect(hookTarget("https://billing.example.com/base")).toMatchObject({ kind: "url" });