// THE VIDEO ASSISTANT'S PROPOSALS FILE (release 38 lane E): // `/assist-proposals.json`, beside the project's video.manifest.json. // // { "version": 1, "promptVersion": "video-assist-1", "at": ISO, "model": "qwen3:8b", // "request": "make the joins 0.15 s with less smear", "scope": "look", "note"?: "n_…", // "reply"?: "…", a sentence the model added (shown, never applied) // "ops": [ { "id": "a1", "op": {…}, "diff": "join 0.22 s → 0.15 s", "warning"?: "…", // "status": "proposed" | "applied" | "dismissed" | "failed", "at"?: ISO, "error"?: "…" } ], // "refused": [ { "op": {…}, "why": "…" } ], // "diff": [ "join 0.22 s → 0.15 s", … ] } // // Written whole by the editor's `video-assist` task (one ask's proposals: a // new ask replaces the file), and its ops' `status` marked by umtool's Apply // and Dismiss. PROPOSALS ONLY: nothing in this file is applied by being here; // Apply re-checks each op against the manifest as it is then (umtool // lib/report/videoAssist.mjs) and writes through the manifest's writers. // // Tmp + rename, so a reader never sees half a file. Plain ESM with a .d.mts. import { readFile, rename, writeFile } from "node:fs/promises"; import path from "node:path"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; export const ASSIST_PROPOSALS_NAME = "assist-proposals.json"; export const ASSIST_PROMPT_VERSION = "video-assist-1"; export const ASSIST_OP_STATUSES = Object.freeze(["proposed", "applied", "dismissed", "failed"]); const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const fileOf = (dir) => path.join(/* turbopackIgnore: true */ dir, ASSIST_PROPOSALS_NAME); /** * The file's document from one ask: the checked ops (videoAssistOps.mjs * checkAssistOps), each given an id and the status "proposed". */ export function proposalsDocOf({ at, model, request, scope = "all", note = null, reply = null, checked }) { const ops = (checked?.ops ?? []).map((k, i) => ({ id: `a${i + 1}`, op: k.op, diff: k.diff, ...(k.warning ? { warning: k.warning } : {}), status: "proposed", })); return { version: 1, promptVersion: ASSIST_PROMPT_VERSION, at, model, request, scope, ...(note ? { note } : {}), ...(reply ? { reply: String(reply).replace(/\s+/g, " ").trim().slice(0, 600) } : {}), ops, refused: (checked?.refused ?? []).map((r) => ({ op: r.op, why: r.why })), diff: ops.map((o) => o.diff), }; } /** The file's problems (empty: it can be read as proposals). */ export function proposalsErrors(doc) { if (!isObj(doc)) return ["not an object"]; const errors = []; if (doc.version !== 1) errors.push("version is not 1"); if (typeof doc.request !== "string") errors.push("no request"); if (!Array.isArray(doc.ops)) errors.push("no ops list"); else doc.ops.forEach((o, i) => { if (!isObj(o) || typeof o.id !== "string" || !isObj(o.op) || typeof o.diff !== "string" || !ASSIST_OP_STATUSES.includes(o.status)) errors.push(`ops[${i}] is not { id, op, diff, status }`); }); if (!Array.isArray(doc.refused)) errors.push("no refused list"); return errors; } /** `{ doc, error }`: the proposals, or null (no file, or one that is not proposals: `error` says why). */ export async function readAssistProposals(dir) { let raw; try { raw = await readFile(fileOf(dir), "utf8"); } catch { return { doc: null, error: null }; } try { const doc = JSON.parse(raw); const errors = proposalsErrors(doc); return errors.length ? { doc: null, error: `${ASSIST_PROPOSALS_NAME}: ${errors.join("; ")}` } : { doc, error: null }; } catch (e) { return { doc: null, error: `${ASSIST_PROPOSALS_NAME}: ${e instanceof Error ? e.message : String(e)}` }; } } /** Write the whole file (tmp + rename). Returns its path. */ export async function writeAssistProposals(dir, doc) { const errors = proposalsErrors(doc); if (errors.length) throw new Error(`not proposals: ${errors.join("; ")}`); const file = fileOf(dir); const tmp = tmpPathFor(file); await writeFile(tmp, `${JSON.stringify(doc, null, 2)}\n`, "utf8"); await rename(tmp, file); return file; } /** The doc with each op of `marks` (`{ [id]: { status, error? } }`) marked at `at`; ids not there are ignored. */ export function markProposals(doc, marks, at) { return { ...doc, ops: doc.ops.map((o) => { const m = marks[o.id]; if (!m) return o; const { error: _e, ...rest } = o; return { ...rest, status: m.status, at, ...(m.error ? { error: String(m.error).slice(0, 400) } : {}) }; }), }; }