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
Queue: note when a person's open page follows their runs
Adarsh8eb4ff4eb21b
diff --git a/src/proxy.ts b/src/proxy.tsindex 9ec98f3d..08870b41 100644--- a/src/proxy.ts+++ b/src/proxy.ts@@ -38,6 +38,7 @@ import { accountRecordExists, installRecordExists } from "@/server/records";import { findShare, shareGuard, shareTargetExists } from "@/server/share-access";import { hostedRunsOn } from "@/server/hosted-switch";import { authStoreFailed } from "@/server/auth-store";+import { followsRuns, noteWatching } from "@/server/computer/watching";/*** Paths that work signed out: the login page and its API (and share links: openShare). Sign-up and@@ -271,6 +272,8 @@ const notYours = () => NextResponse.json({ error: ACCOUNT_LIBRARY_MESSAGE }, { sfunction account(request: NextRequest, who: Account): NextResponse {const { pathname, search } = request.nextUrl;+ // Its open page follows its runs (the reads it makes anyway): its runs go first in their turn on this install's GPUs.+ if (followsRuns(request.method, pathname)) noteWatching(who.id);if (ownerOnly(request.method, pathname)) {return NextResponse.json({ error: "These settings aren't part of your account." }, { status: 403 });}diff --git a/src/server/computer/watching.ts b/src/server/computer/watching.tsnew file mode 100644index 00000000..ee3aad17--- /dev/null+++ b/src/server/computer/watching.ts@@ -0,0 +1,47 @@+import "server-only";+import { db } from "../db";++/**+ * Who is watching their runs now: an account whose open page asked about its runs lately. A page of the studio asks+ * while one of its runs waits or works (the runs list behind the tab's job count, the feed, a run's tile, the Library),+ * and stops asking while its tab is hidden, so a page that asked within WATCH_FRESH_MS is one the person has open. The+ * line on this install's GPUs reads it (pool.ts smartLine): a person watching goes first in each round. Nothing new is+ * asked of the browser: the proxy notes the reads the page already makes (noteWatching), at most once every+ * WATCH_NOTE_MS for each account. Kept in the database (relay_watching), so every app process sees the same.+ */++/** A person counts as watching while their page asked about their runs this recently. */+export const WATCH_FRESH_MS = 60_000;+/** The most often one account's watching is written (a page asks every few seconds). */+export const WATCH_NOTE_MS = 15_000;++declare global {+ var __fuobeWatchNoted: Map<string, number> | undefined;+}++/** The reads of a page that follows its runs: the runs list, one run, and a run's live picture. */+const FOLLOWS = /^\/api\/generations(?:\/(?!(?:facets|trash|stream|zip|bulk|local)$)[^/]+(?:\/preview)?)?$/;++/** Is this request a page following its runs (a GET of the runs list, one run, or a run's live picture)? */+export const followsRuns = (method: string, pathname: string): boolean => (method === "GET" || method === "HEAD") && FOLLOWS.test(pathname);++/** An account's page asked about its runs now. Best-effort: it never fails the request. */+export function noteWatching(account: string, now = Date.now()): void {+ const noted = (globalThis.__fuobeWatchNoted ??= new Map());+ const last = noted.get(account);+ if (last !== undefined && now - last < WATCH_NOTE_MS && now >= last) return;+ try {+ db().prepare("INSERT INTO relay_watching (account, seen_at) VALUES (?, ?) ON CONFLICT(account) DO UPDATE SET seen_at = MAX(seen_at, excluded.seen_at)").run(account, now);+ noted.set(account, now);+ if (noted.size > 5000) noted.delete(noted.keys().next().value!);+ } catch {}+}++/** The accounts watching their runs now (their page asked within WATCH_FRESH_MS). */+export function watchingNow(now = Date.now()): Set<string> {+ try {+ return new Set((db().prepare("SELECT account FROM relay_watching WHERE seen_at > ?").all(now - WATCH_FRESH_MS) as { account: string }[]).map((r) => r.account));+ } catch {+ return new Set();+ }+}diff --git a/src/server/db.ts b/src/server/db.tsindex 1a3e12a1..772635f7 100644--- a/src/server/db.ts+++ b/src/server/db.ts@@ -719,6 +719,7 @@ export const WORKER_JOBS_KEEP_MS = 365 * 24 * 60 * 60_000;* the files staged for a job, under the names ComfyUI gets. worker_jobs: which worker took which task and when, and what* became of it (rows are only added; each is kept a year, like the admin API's log). worker_events: each worker's state* changes, handoffs, stages, claims, admin actions and its own log lines (worker-events.ts; pruned after 7 or 30 days).+ * relay_watching: when an account's open page last asked about its runs (watching.ts: its runs go first in their turn).* Safe to run on every start.*/export const RELAY_TABLES = `CREATE TABLE IF NOT EXISTS relay_runners (@@ -864,6 +865,10 @@ export const RELAY_TABLES = `CREATE TABLE IF NOT EXISTS relay_runners (CREATE TRIGGER IF NOT EXISTS worker_jobs_kept BEFORE DELETE ON worker_jobsWHEN OLD.at > CAST(strftime('%s', 'now') AS INTEGER) * 1000 - ${WORKER_JOBS_KEEP_MS}BEGIN SELECT RAISE(ABORT, 'worker_jobs rows are kept a year'); END;+ CREATE TABLE IF NOT EXISTS relay_watching (+ account TEXT PRIMARY KEY,+ seen_at INTEGER NOT NULL+ );CREATE TABLE IF NOT EXISTS worker_events (id INTEGER PRIMARY KEY AUTOINCREMENT,at INTEGER NOT NULL,