import { NextResponse } from "next/server"; import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { connectedArchives } from "yt-dlp-transcript-common/lib/remoteArchives"; import { ArchiveProbeError } from "yt-dlp-transcript-common/lib/remoteArchives-server"; import { RemoteArchiveError, connectRemoteArchive, removeRemoteArchive, type RemoteArchiveDeps, } from "yt-dlp-transcript-common/controller/remoteArchives"; import { saveSettings } from "../../../settings/saveSettings"; import { OpsInputError, ops, opsFail, optBool, optString } from "../_lib"; import { readRoute } from "../_read"; export const dynamic = "force-dynamic"; // THE CONNECTED ARCHIVES (demo epic D1; lib/remoteArchives.ts): the published // archives this machine reads as sources — Create, research and the MCP offer // each, a sweep over one cites it, and a window it cites registers a // remote-only channel to fetch it. // // GET /api/ops/archives // -> { ok, archives: [{ id, origin, title, addedAt, fromEnv? }] } // (`fromEnv`: TRANSCRIPT_SITE_URL's, listed and never stored) // POST { connect: "", dryRun? } // -> { ok, archive, probe: { origin, siteId, title, spec, generatedAt, // totals, channels }, already } // reads /corpus.json once; a hub, a cited-only site, a site with // no channels or an origin that does not answer is refused (400/502). // dryRun answers the probe and writes nothing (already: null); an origin // connected already writes nothing (already: true). // POST { remove: "" } -> { ok, removed } const deps = (): RemoteArchiveDeps => ({ stored: () => getSettings().remoteArchives, save: (next) => saveSettings({ remoteArchives: next }), }); export async function GET(request: Request) { return readRoute(request, [], async () => ({ archives: connectedArchives(getSettings().remoteArchives) })); } export async function POST(request: Request) { return ops(request, ["connect", "remove", "dryRun"], async (body) => { const connect = optString(body, "connect")?.trim(); const remove = optString(body, "remove")?.trim(); if (!!connect === !!remove) throw new OpsInputError('send "connect" (an origin) or "remove" (an id or origin)'); try { if (connect) { const r = await connectRemoteArchive({ origin: connect, dryRun: optBool(body, "dryRun") === true }, deps()); return NextResponse.json({ ok: true, ...r }); } if (optBool(body, "dryRun") !== undefined) throw new OpsInputError('"dryRun" goes with "connect"'); return NextResponse.json({ ok: true, removed: await removeRemoteArchive(remove!, deps()) }); } catch (e) { if (e instanceof ArchiveProbeError) return opsFail(e.message, e.status); if (e instanceof RemoteArchiveError) return opsFail(e.message); throw e; } }); }