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.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
import { callerKey, principalOf, rateOf, relayAnswer, requestWorker } from "@/server/computer/http";
import { workerClaim } from "@/server/computer/pool";
import { claim } from "@/server/computer/tasks";
import { refreshTicketsBeforeClaim } from "@/server/computer/ticket-refresh";
/**
* The person's Fuobe tab asks for a job to run on its ComfyUI: one it started before and lost track of (a reload), or,
* when this computer runs the waiting jobs, the oldest one. The answer is the graph exactly as the server built it (for
* an account's run on the workers, its job ticket: src/lib/job-ticket.ts), the inputs to send under the names it chose, the prompt id it minted, and a new token for every call about it. No job:
* 204.
*/
export async function POST(request: Request) {
const principal = principalOf(request);
if (principal instanceof Response) return principal;
const body = (await request.json().catch(() => null)) as { device?: unknown; running?: unknown; slot?: unknown; noStream?: unknown } | null;
// Each of a worker's slots claims for itself, each counted on its own: an idle CPU slot never uses up its GPU's claims.
const limited = rateOf(callerKey(request, principal), body?.slot === "cpu" ? "claim-cpu" : "claim", 60);
if (limited) return limited;
const running = Array.isArray(body?.running) ? body.running.filter((x): x is string => typeof x === "string").slice(0, 32) : [];
try {
const worker = requestWorker(request);
// Job tickets made for the catalog before this version of the studio are made again first (ticket-refresh.ts).
if (worker?.kind === "pool") await refreshTicketsBeforeClaim();
// A shared GPU takes the best task it can run from its pool (pool.ts), for its GPU or for its CPU slot (`slot`:
// "cpu"); a person's computer, their own oldest. A worker whose PC's memory is in use (`noStream`, its RAM guard)
// prefers a job whose weights fit whole on its card, and takes one it streams when no other worker could now.
const task = worker?.kind === "pool" ? workerClaim(worker, running, Date.now(), body?.slot === "cpu" ? "cpu" : "gpu", { noStream: body?.noStream === true }) : claim(principal, String(body?.device ?? ""), running);
if (!task) return new Response(null, { status: 204, headers: { "Cache-Control": "no-store" } });
return Response.json({ task }, { headers: { "Cache-Control": "no-store" } });
} catch (e) {
return relayAnswer(e);
}
}