import type { NextResponse } from "next/server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { liveRecordingsAttentionCounts } from "yt-dlp-transcript-common/controller/liveRecordings"; // WHAT NEEDS A PERSON, ON EVERY REPLY OF THE OPS DOOR. An agent polling the // editor reads replies, not pages: a live recording that failed (or lost its // start, was interrupted, kept parts it did not land), and one that runs but // has stopped growing, is said in a header on every answer the door gives — // `ops()`, `readRoute()`, and the GET routes that check opsAuth themselves // (they wrap their answer in withAttention) — // // x-archilyzer-attention: live-recordings=[, stalled=] // // — only when N or M is above 0, and `pnpm ops` prints one stderr line for it. // The counts (controller/liveRecordings.ts) are cached 30 s: they read each // channel's live-recordings.json, and a reply must not wait for that every // time. A count that could not be read is not cached (and not said). export const ATTENTION_HEADER = "x-archilyzer-attention"; const TTL_MS = 30_000; type Counts = { needsAttention: number; stalled: number }; let cached: { at: number; counts: Counts } | null = null; export async function liveRecordingsAttention(now = Date.now()): Promise { if (cached && now - cached.at < TTL_MS) return cached.counts; try { const counts = await liveRecordingsAttentionCounts(getPaths()); cached = { at: now, counts }; return counts; } catch { return null; } } // For a write that changed the answer (an acknowledgement): the next reply counts again. export function forgetAttention(): void { cached = null; } export function attentionHeaderValue(c: Counts | null): string | null { if (!c || (c.needsAttention <= 0 && c.stalled <= 0)) return null; return `live-recordings=${c.needsAttention}${c.stalled > 0 ? `, stalled=${c.stalled}` : ""}`; } export async function withAttention(res: R): Promise { try { const v = attentionHeaderValue(await liveRecordingsAttention()); if (v) res.headers.set(ATTENTION_HEADER, v); } catch { /* a reply is never refused for its header */ } return res; }