import { resolveReport } from "@/lib/report/serve.mjs"; import { cuesForSource } from "@/lib/projects/report.mjs"; export const dynamic = "force-dynamic"; // ADD A CLIP's "find the moment" in the recordings the cut already draws on // (lane/ui-simplify, 5 Nov): every word of `q` (any order, any case) in one // cue of a recording a clip of the cut is on, read from the cue files the cut // reads (cuesForSource, lib/projects/report.mjs), never the network. The // archive-wide search is /api/research/search; a pasted link needs neither. // // GET ?project&q -> { recordings, hits: [{ channel, video, title, date, t, end, text }] } (at most 80) export async function GET(request: Request) { const url = new URL(request.url); const r = await resolveReport(url.searchParams.get("project") ?? ""); if ("error" in r) return Response.json({ error: r.error }, { status: r.status }); const words = (url.searchParams.get("q") ?? "") .toLowerCase() .split(/\s+/) .map((w) => w.replace(/[^\p{L}\p{N}'-]/gu, "")) .filter((w) => w.length > 0); if (!words.length) return Response.json({ error: "type a word or two to find" }, { status: 400 }); type Rec = { channel: string | null; video: string; title: string | null; date: string | null }; const recs = new Map(); for (const e of (r.manifest.timeline ?? []) as Record[]) { if (e?.type !== "clip" || typeof e.video !== "string") continue; const channel = typeof e.channel === "string" && e.channel ? e.channel : null; const key = `${channel ?? ""}/${e.video}`; if (recs.has(key)) continue; const onscreen = (e.onscreen ?? null) as { title?: unknown } | null; const title = typeof e.streamTitle === "string" ? e.streamTitle : typeof onscreen?.title === "string" ? onscreen.title : typeof e.title === "string" ? e.title : null; recs.set(key, { channel, video: e.video, title, date: typeof e.date === "string" ? e.date.slice(0, 10) : null }); } const hits: (Rec & { t: number; end: number; text: string })[] = []; for (const rec of recs.values()) { if (hits.length >= 80) break; const got = await cuesForSource(r.project.dir, { video: rec.video, channel: rec.channel }, 0, Number.MAX_SAFE_INTEGER).catch(() => null); for (const c of got?.cues ?? []) { const text = String(c.text ?? "").toLowerCase(); if (words.every((w) => text.includes(w))) { hits.push({ ...rec, t: c.start, end: c.end, text: String(c.text ?? "").replace(/\s+/g, " ").trim() }); if (hits.length >= 80) break; } } } return Response.json({ recordings: recs.size, hits }, { headers: { "cache-control": "no-store" } }); }