Every useLive() call opened its own EventSource, and the library page and a model's Jobs tab call it twice. Over plain HTTP a browser allows 6 connections per host, so with three library tabs open the third took ~10 s to load and its requests never got an answer; a fourth tab didn't load. - /api/live answers one short request with the job snapshot (left out when the client already has that version) and, for job pages, new log lines. - src/lib/live-store.ts runs one poller per query per tab, shared by every component showing it, paused while the tab is hidden, with backoff on errors. One router.refresh() per finished job instead of one per component. - The worker dot reads the status from that poll; it only asks /api/status (every 10 s) when nothing else is polled. - The worker status no longer carries the heartbeat time (it changed every 5 s, so every idle stream re-sent its snapshot) and rounds free space to MiB. - Removes /api/stream: its sleep() added an abort listener per second to the request signal and never removed it. - Job actions poll right away after starting or cancelling a job; drops approveJob/rejectJob, whose routes went away in 0.15. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
39 lines
1.8 KiB
TypeScript
39 lines
1.8 KiB
TypeScript
// The /api/live protocol, shared by the route handler and the browser's live store. Types only and
|
|
// pure helpers: safe to import from client components.
|
|
|
|
import type { JobFileSnapshot, JobSnapshot, LogLine } from "@/lib/jobs";
|
|
import type { WorkerStatus } from "@/lib/worker-status";
|
|
|
|
export type LiveSnapshot = { worker: WorkerStatus; jobs: JobSnapshot[]; files: JobFileSnapshot[] };
|
|
|
|
/** `model`: jobs of one model; `job`: one job; neither: open and recently finished jobs. */
|
|
export type LiveQuery = { model?: string; job?: string };
|
|
|
|
/** `snapshot` is left out when it is the version the client already has (`v` in the request). */
|
|
export type LiveResponse = { v: string; snapshot?: LiveSnapshot; logs?: LogLine[] };
|
|
|
|
/**
|
|
* The poll URL. `logAfter` (job queries only): log lines after that id, 0 for the last ones; leave it
|
|
* out for no logs. `v`: the snapshot version the client has.
|
|
*/
|
|
export function liveUrl(query: LiveQuery, opts: { logAfter?: number | null; v?: string | null } = {}): string {
|
|
const p = new URLSearchParams();
|
|
if (query.model) p.set("model", query.model);
|
|
if (query.job) p.set("job", query.job);
|
|
if (query.job && opts.logAfter != null) p.set("logAfter", String(opts.logAfter));
|
|
if (opts.v) p.set("v", opts.v);
|
|
const qs = p.toString();
|
|
return qs ? `/api/live?${qs}` : "/api/live";
|
|
}
|
|
|
|
/**
|
|
* The response body, built around the already serialized snapshot so it isn't serialized twice.
|
|
* `version` is a hash of `snapshotJson`.
|
|
*/
|
|
export function liveBody(snapshotJson: string, version: string, clientVersion: string | null, logs?: LogLine[]): string {
|
|
let body = `{"v":${JSON.stringify(version)}`;
|
|
if (clientVersion !== version) body += `,"snapshot":${snapshotJson}`;
|
|
if (logs) body += `,"logs":${JSON.stringify(logs)}`;
|
|
return `${body}}`;
|
|
}
|