import path from "node:path"; import { applyRespell, planRespell, wordHunks } from "yt-dlp-transcript-common/lib/report/respellArticle"; import { ArticleRegistryError } from "yt-dlp-transcript-common/lib/report/registry"; import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; import { errorResponse, projectsRootOf, readArticleIterations, readPolicy } from "@/lib/articles/generation"; import { sitesPaths } from "@/lib/articles/sites"; import { configuredProjectsRoot } from "@/lib/report/chain.mjs"; import { clearPiecesCache } from "@/lib/pieces"; import type { RespellView } from "@/lib/articles/hand-types"; export const dynamic = "force-dynamic"; // FIX A MISHEARING (release 32 R2): `archilyzer reports respell` from the // article page, through the same two functions (common's respellArticle.ts). // // POST {article, heard: string[] | "a, b", reads, bracket?, note?, iteration?, apply?} // apply absent/false -> { plan } the preview: every change per file, // as word hunks («before» → «after») // apply: true -> { plan, backups } applied (the plan is computed // again), each changed file backed up // first, as the CLI does // Each answer names the iteration's video project (`projectId`) when it has // one: a respelled video shows the new words once it is rebuilt. const NO_STORE = { "cache-control": "no-store" }; async function projectsRoot(): Promise { try { return projectsRootOf((await readPolicy()).generation); } catch { return configuredProjectsRoot(); // the editor unreachable: settings.json, as the chain reads it } } const heardOf = (v: unknown): string[] => (Array.isArray(v) ? v : typeof v === "string" ? v.split(",") : []).map((s) => String(s).trim()).filter(Boolean); // A file as the operator reads it: under the article's folder, or under the projects root. function shown(file: string, article: string, root: string): string { const a = path.relative(path.join(sitesPaths().articlesDir, article), file); if (!a.startsWith("..") && !path.isAbsolute(a)) return a; const p = path.relative(root, file); return !p.startsWith("..") && !path.isAbsolute(p) ? p : file; } export async function POST(request: Request) { const body = (await request.json().catch(() => ({}))) as Record; const article = String(body.article ?? ""); if (!isReportId(article)) return Response.json({ error: `"${article}" is not an article id` }, { status: 400, headers: NO_STORE }); const reads = typeof body.reads === "string" ? body.reads.trim() : ""; const heard = heardOf(body.heard); if (!heard.length || !reads) return Response.json({ error: "give the heard forms and what was said" }, { status: 400, headers: NO_STORE }); const iteration = body.iteration === undefined || body.iteration === null || body.iteration === "" ? undefined : Number(body.iteration); if (iteration !== undefined && !(Number.isInteger(iteration) && iteration > 0)) return Response.json({ error: "iteration is an iteration number" }, { status: 400, headers: NO_STORE }); const respelling = { heard, reads, ...(body.bracket === true ? { bracket: true } : {}), ...(typeof body.note === "string" && body.note.trim() ? { note: body.note.trim() } : {}), }; const root = await projectsRoot(); const opts = { projectsRoot: root, paths: sitesPaths(), ...(iteration !== undefined ? { iteration } : {}) }; try { const out = body.apply === true ? await applyRespell(article, [respelling], opts) : { plan: await planRespell(article, [respelling], opts), backups: undefined }; const its = await readArticleIterations(article).catch(() => null); const projectId = out.plan.iteration !== null ? (its?.iterations.find((r) => r.n === out.plan.iteration)?.projectId ?? null) : null; const view: RespellView = { article, iteration: out.plan.iteration, respellings: out.plan.respellings, total: out.plan.total, notes: out.plan.notes, targets: out.plan.targets.map((t) => ({ kind: t.kind, file: shown(t.file, article, root), changes: t.changes.map((c) => ({ at: c.at, hunks: wordHunks(c.before, c.after) })), })), projectId, ...(out.backups ? { backups: out.backups.map((b) => shown(b, article, root)) } : {}), }; if (body.apply === true) clearPiecesCache(); return Response.json(view, { headers: NO_STORE }); } catch (err) { if (err instanceof ArticleRegistryError) return Response.json({ error: err.message, problems: err.problems ?? [] }, { status: 400, headers: NO_STORE }); return errorResponse(err); } }