// TASK youtube — the upload's title and summary (release 28, M6): // `publish.youtube { title, description }`. A STEP on a done iteration, on the // media task scaffold (./media.ts): one unit, the whole field. // // The model writes the title and ONE summary paragraph; the build composes // out/youtube.txt from them (umtool/report-to-video/youtube.mjs: the chapters // from its own chapter list, a moment link per cited clip), so the summary // leaves room for those under YouTube's 5000. CODE checks: // - the title is one line of at most PUBLISH_YOUTUBE_LIMITS.title // characters; the summary one paragraph of at most YOUTUBE_SUMMARY_MAX; // - grounded (./grounding.ts, ./clipEvidence.ts checkLine): every name and // number is the evidence's (the report's title, summary, sections, claims // and quotes, the cut's title and subtitle); no stock phrase; third person // (Q1: a first-person word outside quotation marks is refused); words in // quotation marks only when a cited quote says them word for word; // - no links, timestamps or chapter list: those are code's. // A summary failing a check loses its failing sentences when what is left // stands (the repair); the better of two attempts is kept. import { PUBLISH_YOUTUBE_LIMITS } from "../../drafted.mjs"; import { sentencesOf, type GroundingInput } from "../grounding"; import type { VideoManifest } from "../videoOps"; import { checkLine, fitLines } from "./clipEvidence"; import { runMediaTask, type MediaAttempt, type MediaContext, type MediaTaskInput, type MediaTaskSpec, type MediaUnit } from "./media"; import type { TaskDeps, TaskResult } from "./types"; export type YoutubeInput = MediaTaskInput; export type YoutubeValue = { title: string; description: string }; /** The summary's limit: room under YouTube's 5000 for the chapters and the sources. */ export const YOUTUBE_SUMMARY_MAX = 900; type YoutubeUnit = MediaUnit & { g: GroundingInput; evidence: string }; const EVIDENCE_BUDGET = 12_000; const oneLine = (s: unknown) => String(s ?? "").replace(/\s+/g, " ").trim(); const stripCites = (s: string) => s.replace(/\[([^\]]*)\]\(cite:[^)]+\)/g, "$1").replace(/\(cite:[^)]+\)/g, ""); const LINK = /https?:\/\/|www\.|\b\d{1,2}:\d{2}\b|#\w/; /** The title's problems. */ export function titleProblems(title: string, g: GroundingInput): { hard: string[]; soft: string[] } { const hard: string[] = []; if (!title) hard.push("the title: empty"); else if (title.length > PUBLISH_YOUTUBE_LIMITS.title) hard.push(`the title is ${title.length} characters (at most ${PUBLISH_YOUTUBE_LIMITS.title})`); if (LINK.test(title)) hard.push("the title: no links, timestamps or hashtags"); const r = title ? checkLine(title, g, { quotes: true }) : { hard: [], soft: [] }; return { hard: [...hard, ...r.hard.map((h) => `the title: ${h}`)], soft: r.soft.map((s) => `the title: ${s}`) }; } /** The summary's problems. */ export function summaryProblems(text: string, g: GroundingInput): { hard: string[]; soft: string[] } { const hard: string[] = []; if (!text) hard.push("the summary: empty"); else if (text.length > YOUTUBE_SUMMARY_MAX) hard.push(`the summary is ${text.length} characters (at most ${YOUTUBE_SUMMARY_MAX})`); if (LINK.test(text)) hard.push("the summary: no links, timestamps, chapters or hashtags — the build adds the chapters and the sources"); const r = text ? checkLine(text, g, { quotes: true }) : { hard: [], soft: [] }; return { hard: [...hard, ...r.hard.map((h) => `the summary: ${h}`)], soft: r.soft.map((s) => `the summary: ${s}`) }; } export function youtubeSystem(): string { return ( "You write the YouTube title and the summary paragraph of the description for a short evidence video made from a " + `cited report. Rules: (1) the title is one line of at most ${PUBLISH_YOUTUBE_LIMITS.title} characters: plain and ` + "specific, no clickbait, no all-caps, no emoji; (2) the summary is one paragraph of at most " + `${YOUTUBE_SUMMARY_MAX} characters saying what the video shows and what the evidence found; (3) third person: you ` + "describe what the speaker said, never as the speaker (no I, me, my, we, us or our outside quotation marks); (4) every " + "name and number comes from the evidence, and nothing goes beyond it; (5) words in quotation marks only when a quote " + "below says them word for word; (6) no links, timestamps, chapter list or hashtags: the chapters and the sources are " + "added after it." ); } /** * The evidence a title and summary may draw on, and its grounding (the cut's * title and subtitle added). The social posts (./social.ts) draw on the same. */ export function evidenceOf(ctx: MediaContext): { text: string; g: GroundingInput } { const r = ctx.report as { title?: string; summary?: string | null; sections?: { title?: string; claims?: { text?: string; verdict?: string }[] | null }[]; citations?: Record | null; }; const m = ctx.manifest as VideoManifest; const head = [`Report title: ${r.title ?? ""}`]; if (m.title && m.title !== r.title) head.push(`Video title: ${m.title}`); if (m.subtitle) head.push(`Video subtitle: ${m.subtitle}`); if (r.summary) head.push(`Report summary: ${oneLine(r.summary).slice(0, 1200)}`); const lines: string[] = []; for (const s of r.sections ?? []) { const claims = (s.claims ?? []).map((c) => ` - ${oneLine(stripCites(c.text ?? "")).slice(0, 220)}${c.verdict ? ` [${c.verdict}]` : ""}`); if (!claims.length) continue; lines.push(`Section: ${s.title ?? ""}`, ...claims); } const ev = fitLines(lines, EVIDENCE_BUDGET); const quotes = [...new Set(Object.values(r.citations ?? {}).map((c) => oneLine(c?.quote)).filter(Boolean))]; const q = fitLines(quotes.map((x) => `- "${x.slice(0, 200)}"`), 4000); return { text: `${head.join("\n")}\n\nWhat it found:\n${ev.text}` + (ev.omitted ? `\n(${ev.omitted} more line(s) not shown)` : "") + (q.text ? `\n\nQuotes (the only words you may put in quotation marks):\n${q.text}` : ""), g: { ...ctx.grounding, reference: [ctx.grounding.reference, m.title ?? "", m.subtitle ?? ""].join("\n") }, }; } export const YOUTUBE_SPEC: MediaTaskSpec = { task: "youtube", noun: "YouTube text", units(ctx) { const { text, g } = evidenceOf(ctx); return [{ id: "youtube", target: { field: "publish.youtube" }, label: "YouTube title and summary", g, evidence: text }]; }, schema: () => ({ type: "object", properties: { title: { type: "string" }, description: { type: "string" } }, required: ["title", "description"], }), system: () => youtubeSystem(), user: (u) => `${u.evidence}\n\nWrite the title and the summary.`, parse(answer) { const a = answer as { title?: unknown; description?: unknown } | null; return { title: oneLine(a?.title).replace(/^["“]+|["”]+$/g, ""), // One paragraph: line breaks are spaces. description: oneLine(a?.description), }; }, check(v, u) { const t = titleProblems(v.title, u.g); const d = summaryProblems(v.description, u.g); return { hard: [...t.hard, ...d.hard], soft: [...t.soft, ...d.soft] }; }, // A clean title, with the summary's failing sentences removed (never // rewritten) and cut to the limit at a sentence. repair(attempts, u) { let best: MediaAttempt | null = null; for (const a of attempts) { if (!a.value || titleProblems(a.value.title, u.g).hard.length) continue; const kept: string[] = []; for (const s of sentencesOf(a.value.description)) { if (summaryProblems(s, u.g).hard.length) continue; if ([...kept, s].join(" ").length > YOUTUBE_SUMMARY_MAX) break; kept.push(s); } const description = kept.join(" "); if (!description) continue; const v = { title: a.value.title, description }; const again = summaryProblems(description, u.g); if (again.hard.length) continue; if (!best || description.length > best.value!.description.length) { best = { value: v, hard: [], soft: [...titleProblems(v.title, u.g).soft, ...again.soft, "trimmed: failing or overlong sentences removed"] }; } } return best; }, describe: (v) => `${v.title} — ${v.description.slice(0, 120)}${v.description.length > 120 ? "…" : ""}`, }; export async function youtube(input: YoutubeInput, deps: TaskDeps): Promise { return runMediaTask(YOUTUBE_SPEC, input, deps); }