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.

Refusals: a refused upload's card says "add your own files", not "run this"

Adarsh503c49e0c7a5
diff --git a/src/components/guest-prompt.tsx b/src/components/guest-prompt.tsx
index b2b54d23..35dea01f 100644
--- a/src/components/guest-prompt.tsx
+++ b/src/components/guest-prompt.tsx
@@ -11,16 +11,17 @@ import { takeGpuRetry } from "@/hooks/use-gpu";
import { KeyInstead } from "@/components/keys/your-keys";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
-import { CONNECT_GPU_EVENT, GUEST_PROMPT_EVENT, refusalCard, signInHref, useStatus, useViewer, watchGuestRefusals, type RefusalCard } from "@/lib/api";
+import { CONNECT_GPU_EVENT, GUEST_PROMPT_EVENT, refusalCard, signInHref, useStatus, useViewer, watchGuestRefusals, type RefusalAction, type RefusalCard, type RefusalDetail } from "@/lib/api";
import { CLOUD_KEY } from "@/lib/your-keys";
/**
* The cards a public studio shows when something is refused (watchGuestRefusals): "Create a free
- * account to run this" for a guest (public sign-up, signed out) who tries to generate, upload or
- * change anything, and "Connect your GPU to run this" for an account (accounts run on their own
- * ComfyUI, or their own fal.ai or Replicate key, never the owner's keys or GPU), with "No GPU? Add
- * a key" when it has no key yet. Mounted once, in the app's providers, so it outlives the page: a
- * card closes when the page changes (its own Sign up or Log in, Back), and never opens on the
+ * account to run this" for a guest (public sign-up, signed out) who tries to generate or change
+ * anything, and "Connect your GPU to run this" for an account (accounts run on their own ComfyUI,
+ * or their own fal.ai or Replicate key, never the owner's keys or GPU), with "No GPU? Add a key"
+ * when it has no key yet. A refused upload, or a file chooser that asks first (useAskBeforeUpload),
+ * says "add your own files" instead. Mounted once, in the app's providers, so it outlives the page:
+ * a card closes when the page changes (its own Sign up or Log in, Back), and never opens on the
* sign-in page (refusalCard). Opened by a run that was refused, "Connect your GPU" keeps that run
* and starts it by itself once the GPU is connected. Refused as a guest while this page still had
* someone signed in (the password changed in another browser), it says "You were signed out" instead.
@@ -31,6 +32,8 @@ export function GuestPrompt() {
const [open, setOpen] = useState(false);
// Opened by the runner's note: ComfyUI needs Fuobe's setting again, find ComfyUI, or let this browser reach it.
const [reason, setReason] = useState<FindReason | undefined>(undefined);
+ // What was refused: a run (or any other change), or a file of the visitor's own.
+ const [action, setAction] = useState<RefusalAction>("run");
const [retry, setRetry] = useState<{ retry: () => void; kind?: string } | null>(null);
const close = useCallback(() => {
setOpen(false);
@@ -54,10 +57,15 @@ export function GuestPrompt() {
setOpen(true);
return true;
};
- const guest = () => {
+ // A bare Event (the sidebar's "Connect your GPU") has no detail: a run.
+ const detail = (e: Event) => (e as CustomEvent<(RefusalDetail & { reason?: FindReason }) | null>).detail ?? {};
+ const guest = (e: Event) => {
// Signed in a moment ago: not an offer to sign up, but to log in again; and the page asks who is looking.
const out = was.current;
- if (show("guest")) setSignedOut(out);
+ if (show("guest")) {
+ setSignedOut(out);
+ setAction(detail(e).action ?? "run");
+ }
if (out) {
was.current = null;
void qc.invalidateQueries({ queryKey: ["viewer"] });
@@ -65,11 +73,13 @@ export function GuestPrompt() {
}
};
const gpu = (e: Event) => {
+ const { action: refused = "run", reason: why } = detail(e);
// The run that was refused (the composer leaves its retry as it sends): kept until the GPU is connected.
- const r = takeGpuRetry();
+ const r = refused === "run" ? takeGpuRetry() : null;
if (!show("gpu")) return;
setRetry(r);
- setReason((e as CustomEvent<{ reason?: FindReason } | undefined>).detail?.reason);
+ setReason(why);
+ setAction(refused);
};
window.addEventListener(GUEST_PROMPT_EVENT, guest);
window.addEventListener(CONNECT_GPU_EVENT, gpu);
@@ -91,8 +101,14 @@ export function GuestPrompt() {
<div className="mb-1 grid size-10 place-items-center rounded-xl bg-primary text-primary-foreground">
<Cpu className="size-5" />
</div>
- <DialogTitle>{TITLES[reason ?? "run"]}</DialogTitle>
- <DialogDescription>Fuobe runs this on the ComfyUI on your computer. It&apos;s free and private.</DialogDescription>
+ <DialogTitle>{TITLES[reason ?? action]}</DialogTitle>
+ <DialogDescription>
+ {!reason && action === "upload" ? (
+ "A free account keeps its files for runs on its own ComfyUI or its own fal.ai or Replicate key."
+ ) : (
+ <>Fuobe runs this on the ComfyUI on your computer. It&apos;s free and private.</>
+ )}
+ </DialogDescription>
{/* Paid models run on the account's own key instead (Your keys), when the studio allows them. */}
{viewer?.kind === "account" && viewer.paidAllowed !== false && !!viewer.keys?.length && (
<p className="text-[13px] text-muted-foreground">{`Or switch to Cloud to use your ${viewer.keys.map((k) => CLOUD_KEY[k].label).join(" or ")} key.`}</p>
@@ -135,7 +151,7 @@ export function GuestPrompt() {
<div className="mb-1 grid size-10 place-items-center rounded-xl bg-primary text-primary-foreground">
<Sparkles className="size-5" />
</div>
- <DialogTitle>Create a free account to run this</DialogTitle>
+ <DialogTitle>{action === "upload" ? "Create a free account to add your own files" : "Create a free account to run this"}</DialogTitle>
<DialogDescription>You&apos;re browsing as a guest. Sign up with an email and a password to generate, upload and keep your work.</DialogDescription>
</DialogHeader>
<DialogFooter className="gap-2 sm:justify-start">
@@ -165,8 +181,9 @@ function logInAgain(who: "owner" | "account", here?: string): string {
return `${href}${href.includes("?") ? "&" : "?"}owner=1`;
}
-const TITLES: Record<FindReason | "run", string> = {
+const TITLES: Record<FindReason | RefusalAction, string> = {
run: "Connect your GPU to run this",
+ upload: "Connect your GPU or add a key to use your own files",
setting: "Your ComfyUI needs Fuobe's setting again",
find: "Find your ComfyUI",
browser: "Run your GPU jobs from this browser",
diff --git a/src/lib/api.ts b/src/lib/api.ts
index 34fe78a5..a7cd6fcd 100644
--- a/src/lib/api.ts
+++ b/src/lib/api.ts
@@ -1209,6 +1209,26 @@ export const ADD_KEY_EVENT = "fuobe:add-key";
/** A card GuestPrompt shows for a refusal: "Create a free account" (coming back to `here` after) or "Connect your GPU". */
export type RefusalCard = { kind: "guest"; here: string } | { kind: "gpu" };
+/** What a refusal card is about, for its words: a run (or any other change), or adding a file of one's own. */
+export type RefusalAction = "run" | "upload";
+
+/** What the cards' events may say (GUEST_PROMPT_EVENT, CONNECT_GPU_EVENT). Without it, a card is about a run. */
+export interface RefusalDetail {
+ action?: RefusalAction;
+}
+
+/** The requests that add a file of the visitor's own: their refusal's card says "add your own files", not "run this". */
+const UPLOAD_PATHS = new Set(["/api/uploads", "/api/media/probe"]);
+
+/** A request's path, whichever way fetch was given it (a path, a URL or a Request). Empty when it can't be read. */
+function requestPath(input: RequestInfo | URL): string {
+ try {
+ return new URL(input instanceof Request ? input.url : String(input), window.location.href || "http://localhost").pathname;
+ } catch {
+ return "";
+ }
+}
+
/**
* The card a refusal opens on this page. None on the sign-in page: the visitor is signing in already,
* and a wrong password there is the form's own error.
@@ -1224,27 +1244,27 @@ export function refusalCard(kind: RefusalCard["kind"], at: { pathname: string; s
* (a 403 marked x-fuobe-connect-gpu): open "Connect your GPU". Watches every fetch, so the studio's
* own helpers (json() here, and the components' own) all get it. The response is marked
* (src/lib/refusals.ts): the error json() and postFile() throw for it shows no toast on top of the
- * card. Never on the sign-in page (refusalCard). Installed once.
+ * card. Never on the sign-in page (refusalCard). Installed once. A refused upload's card says so ("add your own
+ * files"), not "run this".
*/
export function watchGuestRefusals(): void {
const w = window as typeof window & { __fuobeGuestFetch?: boolean };
if (w.__fuobeGuestFetch) return;
w.__fuobeGuestFetch = true;
const original = window.fetch.bind(window);
- const open = (res: Response, kind: RefusalCard["kind"]) => {
+ const open = (res: Response, kind: RefusalCard["kind"], path: string) => {
if (!refusalCard(kind, window.location)) return;
markRefusalShown(res);
- window.dispatchEvent(new Event(kind === "guest" ? GUEST_PROMPT_EVENT : CONNECT_GPU_EVENT));
+ const detail: RefusalDetail = { action: UPLOAD_PATHS.has(path) ? "upload" : "run" };
+ window.dispatchEvent(new CustomEvent(kind === "guest" ? GUEST_PROMPT_EVENT : CONNECT_GPU_EVENT, { detail }));
};
window.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
const res = await original(input, init);
- if (res.status === 401 && res.headers.get("x-fuobe-guest") === "1") {
- const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
- if (method !== "GET" && method !== "HEAD") open(res, "guest");
- }
- if (res.status === 403 && res.headers.get("x-fuobe-connect-gpu") === "1") open(res, "gpu");
+ const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
+ if (res.status === 401 && res.headers.get("x-fuobe-guest") === "1" && method !== "GET" && method !== "HEAD") open(res, "guest", requestPath(input));
+ if (res.status === 403 && res.headers.get("x-fuobe-connect-gpu") === "1") open(res, "gpu", requestPath(input));
// A run started here, on the ComfyUI on this computer: the tab that runs it asks for it at once.
- if (res.status === 201 && (init?.method ?? "").toUpperCase() === "POST" && new URL(input instanceof Request ? input.url : String(input), window.location.href).pathname === "/api/generations") {
+ if (res.status === 201 && method === "POST" && requestPath(input) === "/api/generations") {
window.dispatchEvent(new Event(COMPUTER_KICK_EVENT));
}
// An account's paid run without its own key for that provider (x-fuobe-add-key: fal | replicate): "Add your key".
diff --git a/tests/guest-prompt.test.ts b/tests/guest-prompt.test.ts
index c46a889b..1637b726 100644
--- a/tests/guest-prompt.test.ts
+++ b/tests/guest-prompt.test.ts
@@ -33,7 +33,7 @@ import { fileToSlot, type SlotValue } from "@/components/studio/media-slot";
import { useCreateSwapProject } from "@/components/swap-studio/hooks";
import { runGpuCheck } from "@/hooks/use-gpu";
import { assetUsage, CONNECT_GPU_EVENT, enhance, GUEST_PROMPT_EVENT, refusalCard, signInHref, watchGuestRefusals } from "@/lib/api";
-import { CONNECT_GPU_HEADER, CONNECT_GPU_MESSAGE, GUEST_HEADER, GUEST_LIBRARY_MESSAGE, GUEST_RUN_MESSAGE } from "@/lib/guest-access";
+import { CONNECT_GPU_HEADER, CONNECT_GPU_MESSAGE, CONNECT_GPU_UPLOAD_MESSAGE, GUEST_HEADER, GUEST_LIBRARY_MESSAGE, GUEST_RUN_MESSAGE } from "@/lib/guest-access";
import { refusalShown, toastError } from "@/lib/refusals";
const answer = (status: number, body: Record<string, unknown>, headers: Record<string, string> = {}) =>
@@ -41,20 +41,25 @@ const answer = (status: number, body: Record<string, unknown>, headers: Record<s
const guestRefusal = () => answer(401, { error: GUEST_RUN_MESSAGE }, { [GUEST_HEADER]: "1" });
let events: string[] = [];
+/** What each card's event said (its detail): what was refused. */
+let details: unknown[] = [];
let server: (url: string, init?: RequestInit) => Response = () => answer(200, {});
/** A page at `pathname` with the watcher installed: `window` is this global, as in a browser. */
function page(pathname: string, search = "") {
vi.stubGlobal("window", globalThis);
- vi.stubGlobal("location", { pathname, search });
- vi.stubGlobal("dispatchEvent", (e: Event) => void events.push(e.type));
- vi.stubGlobal("fetch", async (input: RequestInfo | URL, init?: RequestInit) => server(String(input), init));
+ vi.stubGlobal("location", { pathname, search, href: `http://localhost${pathname}${search}` });
+ vi.stubGlobal("dispatchEvent", (e: Event) => {
+ events.push(e.type);
+ details.push((e as CustomEvent).detail ?? null);
+ });
+ vi.stubGlobal("fetch", async (input: RequestInfo | URL, init?: RequestInit) => server(input instanceof Request ? new URL(input.url).pathname : String(input), init));
watchGuestRefusals();
}
const picture = () => new File([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], "photo.png", { type: "image/png" });
-/** Upload a picture into an image tile, as the Color Grading studio's Image tile does: what the tile was given. */
+/** Upload a picture into an image tile, as Face Swap's Target photo does: what the tile was given. */
async function uploadToTile(): Promise<(SlotValue | null)[]> {
const given: (SlotValue | null)[] = [];
await fileToSlot(picture(), (v) => void given.push(v), "image");
@@ -63,39 +68,52 @@ async function uploadToTile(): Promise<(SlotValue | null)[]> {
beforeEach(() => {
events = [];
+ details = [];
toasts.error.length = 0;
});
afterEach(() => {
vi.unstubAllGlobals();
+ vi.restoreAllMocks();
delete (globalThis as { __fuobeGuestFetch?: boolean }).__fuobeGuestFetch;
});
-describe("a guest's refused upload (Color Grading's Image tile)", () => {
+describe("a guest's refused upload (an image tile: Face Swap's Target photo)", () => {
it("opens one card and shows no error toast on top of it", async () => {
- page("/color-grade");
+ page("/apps/app-face-swap");
server = (url, init) => (url === "/api/uploads" && init?.method === "POST" ? guestRefusal() : answer(200, {}));
const given = await uploadToTile();
expect(events).toEqual([GUEST_PROMPT_EVENT]);
+ // The card is about the file: "Create a free account to add your own files".
+ expect(details).toEqual([{ action: "upload" }]);
expect(toasts.error).toEqual([]);
// The local preview while it uploads, then the empty tile again.
expect(given.map((v) => v && { uploading: v.uploading })).toEqual([{ uploading: true }, null]);
});
it("still toasts any other failure (too big, a server error)", async () => {
- page("/color-grade");
+ page("/apps/app-face-swap");
server = () => answer(413, { error: "That picture is over 25 MB." });
await uploadToTile();
expect(events).toEqual([]);
expect(toasts.error).toEqual(["That picture is over 25 MB."]);
});
- it("an account without its GPU gets Connect your GPU alone", async () => {
- page("/color-grade");
- server = () => answer(403, { error: CONNECT_GPU_MESSAGE, code: "connect-gpu" }, { [CONNECT_GPU_HEADER]: "1" });
+ it("an account without its GPU gets Connect your GPU alone, about its files", async () => {
+ page("/apps/app-face-swap");
+ server = () => answer(403, { error: CONNECT_GPU_UPLOAD_MESSAGE, code: "connect-gpu" }, { [CONNECT_GPU_HEADER]: "1" });
await uploadToTile();
expect(events).toEqual([CONNECT_GPU_EVENT]);
+ expect(details).toEqual([{ action: "upload" }]);
expect(toasts.error).toEqual([]);
});
+
+ it("a clip's facts (read from an upload) count as adding a file too, whichever way fetch is called", async () => {
+ page("/upscale");
+ server = () => guestRefusal();
+ await fetch("/api/media/probe", { method: "POST", body: "{}" });
+ await fetch(new Request("http://localhost/api/uploads", { method: "POST", body: "x" }));
+ expect(details).toEqual([{ action: "upload" }, { action: "upload" }]);
+ });
});
describe("a guest's refused run", () => {
@@ -104,6 +122,8 @@ describe("a guest's refused run", () => {
server = () => guestRefusal();
const e = await enhance("a red coat", "image", "t2i").catch((err: unknown) => err);
expect(events).toEqual([GUEST_PROMPT_EVENT]);
+ // The card is about a run: "Create a free account to run this".
+ expect(details).toEqual([{ action: "run" }]);
expect(e).toBeInstanceOf(Error);
expect((e as Error).message).toBe(GUEST_RUN_MESSAGE);
expect(refusalShown(e)).toBe(true);
@@ -173,6 +193,7 @@ describe("each studio's own fetch helper", () => {
.create({ name: "Untitled canvas" })
.catch((err: unknown) => err);
expect(events).toEqual([CONNECT_GPU_EVENT]);
+ expect(details).toEqual([{ action: "run" }]);
expect(refusalShown(e)).toBe(true);
});