// WHERE A DRAFTED FIELD CAME FROM (plans/umtool-redesign.md, P4b): for every // field a media task drafted, the task's own audit file beside the manifest // (`-draft.json`, narrate's `narration-draft.json`) says how many // attempts it took, which one was kept, and the rule each refused attempt // broke (`attempts[].hard`). The Media tab shows it under the field: "attempt // 2 of 2 chosen · attempt 1 failed: ✕ text in quotation marks". // // Two shapes are read: // - release 28's scaffold (common/lib/report/draft/tasks/media.ts): // `{ task, model, at, units: { : { target: {entry?, field}, attempts: // [{value, hard, soft}], chosen, applied? | skipped? } } }`; // - release 25's narrate file: the units at the top level, keyed by card id, // `{ heading, attempts: [{text, hard, soft}], chosen }`, no model. // // Plain ESM: `node --test` runs it with no TypeScript. Read only. import { readdir, readFile, stat } from "node:fs/promises"; import path from "node:path"; import { draftedFields, reviewAddress, stableStringify } from "yt-dlp-transcript-common/lib/report/drafted.mjs"; import { DRAFT_FILE_RE, draftsView } from "./drafts.mjs"; import { awaitingReview } from "../inbox/drafts.mjs"; const strings = (v) => (Array.isArray(v) ? v.filter((x) => typeof x === "string") : []); const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); /** * One unit's attempts, as the field shows them. * * @param {any} unit * @param {number | null} markAttempt the attempt the manifest's mark names, if any */ export function attemptsOf(unit, markAttempt = null) { const attempts = Array.isArray(unit?.attempts) ? unit.attempts.filter(isObj) : []; if (!attempts.length) return null; let chosen = null; if (isObj(unit.chosen)) { const key = stableStringify(unit.chosen.value ?? unit.chosen.text ?? null); const i = attempts.findIndex((a) => stableStringify(a.value ?? a.text ?? null) === key); if (i >= 0) chosen = i + 1; } if (chosen === null && Number.isInteger(markAttempt) && markAttempt >= 1 && markAttempt <= attempts.length) chosen = markAttempt; return { attempts: attempts.length, chosen, // A repaired value (narrate removes the failing sentences) is none of the attempts. repaired: isObj(unit.chosen) && chosen === null, failed: attempts .map((a, i) => ({ attempt: i + 1, hard: strings(a.hard) })) .filter((a) => a.hard.length > 0), soft: strings((isObj(unit.chosen) ? unit.chosen : attempts[(chosen ?? 1) - 1])?.soft), }; } /** * Every field's provenance from a project's draft files. * * @param {{ file: string, doc: any, mtimeMs?: number | null }[]} files * @param {any} manifest the whole manifest (its `drafted` marks pick between two files claiming one field) * @returns {Record} */ export function provenanceFrom(files, manifest) { const marks = markIndex(manifest); /** @type {Record} */ const out = {}; for (const { file, doc, mtimeMs } of files) { if (!isObj(doc)) continue; const model = typeof doc.model === "string" ? doc.model : null; const at = typeof doc.at === "string" ? doc.at : mtimeMs ? new Date(mtimeMs).toISOString() : null; const units = isObj(doc.units) ? Object.values(doc.units).map((u) => ({ u, target: isObj(u?.target) && typeof u.target.field === "string" ? u.target : null })) : // release 25: the narrate file's units at the top level, keyed by card id. file === "narration-draft.json" ? Object.entries(doc).map(([id, u]) => ({ u, target: isObj(u) && Array.isArray(u.attempts) ? { entry: id, field: "narration" } : null })) : []; for (const { u, target } of units) { if (!target) continue; const address = reviewAddress({ entry: typeof target.entry === "string" && target.entry ? target.entry : null, field: target.field }); const mark = marks.get(address) ?? null; const a = attemptsOf(u, mark?.attempt ?? null); if (!a) continue; const prev = out[address]; // Two files claim it: the one the mark's task wrote wins. const mine = mark && file === (mark.task === "narrate" ? "narration-draft.json" : `${mark.task}-draft.json`); if (prev && !mine) continue; out[address] = { file, model: model ?? mark?.model ?? null, at: mark?.at ?? at, ...a }; } } return out; } /** The manifest's drafted marks by review address. */ function markIndex(manifest) { return new Map(draftedFields(manifest).map((d) => [reviewAddress(d), d.provenance])); } /** * Read a project's draft files and answer `provenanceFrom` over them. A file * that does not parse is skipped (its task wrote it; not this view's to repair). * * @param {string} projectDir * @param {any} manifest */ export async function readFieldProvenance(projectDir, manifest) { let names = []; try { names = (await readdir(projectDir)).filter((n) => DRAFT_FILE_RE.test(n)).sort(); } catch { return {}; } const files = []; for (const file of names) { const p = path.join(projectDir, file); try { const [text, st] = await Promise.all([readFile(p, "utf8"), stat(p)]); files.push({ file, doc: JSON.parse(text), mtimeMs: st.mtimeMs }); } catch { /* skipped */ } } return provenanceFrom(files, manifest); } /** * The fields of a project awaiting review (lib/inbox/drafts.mjs awaitingReview): * the number the piece's Media tab shows, the same the Inbox's drafts card counts. * * @param {string} projectDir * @param {any} manifest */ export async function awaitingReviewOf(projectDir, manifest) { const view = await draftsView(projectDir, manifest); if (!view.rows.some((r) => r.state === "unreviewed" || r.state === "edited")) return []; return awaitingReview(view.rows, await readFieldProvenance(projectDir, manifest)); }