import { momentHref, postLinkHref } from "yt-dlp-transcript-common/lib/report/draft/tasks/links"; import { clock } from "yt-dlp-transcript-common/lib/search/window"; import { addQuote, CaseError } from "./cases.mjs"; import { createCase, mutateBoard, readCase } from "./boards"; import { citationText } from "./facets.mjs"; import { isLocalOnly } from "./rules.mjs"; import { listSources } from "./sources"; import { readRunInputs, runOriginOf } from "./runs"; import { researchCaseHref } from "./hrefs"; import { runBoardId } from "./runs-hrefs"; import type { CaseDoc, QuoteCite } from "./case-types"; // --------------------------------------------------------------------------- // AN AGENT'S FINDINGS AS A BOARD (plans/umtool-redesign.md, R3): "Open as a // board" on a run makes -- or brings up to date -- the research case // `
-run` (piece `c:
-run`), whose quotes are the run's // KEPT findings: every quoted citation of the iteration's report, in its // sections, the finding's claim as the claim line. The same shape as a // person's pin (R2's addQuote: a snapshot of the words with their cite), each // marked `by: "agent"`, so human and agent research meet on one board. // // The words are the run's, as its strict check passed them; the board's // Re-verify reads them against the transcript now, as for any pin. A second // open adds what is new (a cite already on the board is not added twice) and // never touches the operator's claims, notes, sections or order. // // Umtool-side state, as every board: nothing here writes the article store. // --------------------------------------------------------------------------- type Citation = { kind?: string; channel?: string; id?: string; start?: number; end?: number; quote?: string; label?: string; date?: string }; const ymd = (d?: string) => (d && /^\d{4}-\d{2}-\d{2}/.test(d) ? d.slice(0, 10).replace(/-/g, "") : d && /^\d{8}$/.test(d) ? d : undefined); /** The R1 source the board's quotes re-verify through: the run's own when offered, else one serving its links' origin, else the local corpus. */ async function sourceFor(runSource: string | null, origin: string | null): Promise { const offered = await listSources().catch(() => []); if (runSource && offered.some((o) => o.handle === runSource)) return runSource; const byOrigin = origin ? offered.find((o) => o.handle === `remote:${origin.replace(/\/+$/, "")}`) : undefined; if (byOrigin) return byOrigin.handle; return offered[0]?.handle ?? runSource ?? ""; } export function citeOfRun(c: Citation, o: { source: string; origin: string | null }): QuoteCite | null { if (!c.channel || !c.id) return null; const post = c.kind === "post"; if (!post && c.kind !== "video" && c.kind !== "audio") return null; const t = typeof c.start === "number" ? c.start : 0; const end = typeof c.end === "number" && c.end >= t ? c.end : t; const url = o.origin ? (post ? postLinkHref(o.origin, c.channel, c.id) : momentHref(o.origin, c.channel, c.id, t)) : null; const title = post ? `Post in ${c.channel}` : (c.label ?? c.id); const date = ymd(c.date); const iso = date ? `${date.slice(0, 4)}-${date.slice(4, 6)}-${date.slice(6, 8)}` : ""; return { kind: post ? "post" : "video", source: o.source, channel: c.channel, video: c.id, title, ...(date ? { date } : {}), t, end, ...(post ? { track: "post" } : { clock: clock(t) }), url, citation: citationText(post ? { quote: c.quote, title: "", post: { author: c.channel, date: iso }, url } : { quote: c.quote, title, clock: clock(t), url }), ...(isLocalOnly(c.channel) ? { localOnly: true } : {}), }; } export type RunBoardResult = { id: string; href: string; created: boolean; added: number; already: number; skipped: number }; /** Open the run's kept findings as a board (create it, or add what is new). */ export async function openRunBoard(article: string, n: number): Promise { const { it, trace, sweep, report } = await readRunInputs(article, n); if (!report?.sections?.length) throw new CaseError(`${article} i${n} kept no findings yet (no report.json)${it.status !== "done" ? `: it is ${it.status}` : ""}`, 409); const id = runBoardId(article, n); const ran = runOriginOf(trace, sweep); const source = await sourceFor(ran.source, ran.origin); const citations = (report.citations ?? {}) as Record; const question = it.question ?? report.title ?? article; let created = false; try { await readCase(id); } catch (e) { if ((e as { status?: number }).status !== 404) throw e; await createCase({ id, title: report.title ?? question, scope: { source, channels: it.scope?.channels ?? [], dateFrom: it.scope?.from, dateTo: it.scope?.to }, query: question, }); created = true; } const r = await mutateBoard(id, (doc: CaseDoc) => { doc.run = { article, n, at: new Date().toISOString() }; let added = 0; let already = 0; let skipped = 0; for (const s of report.sections ?? []) { let section = doc.sections.find((x) => x.title === s.title); if (!section && (s.claims ?? []).length) { section = { id: `s${++doc.counters.section}`, title: s.title }; doc.sections.push(section); } for (const claim of s.claims ?? []) { for (const cid of claim.citations ?? []) { const c = citations[cid]; const cite = c?.quote ? citeOfRun(c, { source, origin: ran.origin }) : null; if (!c?.quote || !cite) { skipped++; continue; } const out = addQuote(doc, { text: c.quote, cite, claim: claim.text, section: section?.id ?? null, query: question, by: "agent" }) as { duplicate: boolean }; if (out.duplicate) already++; else added++; } } } return { added, already, skipped }; }); return { id, href: researchCaseHref(id), created, ...r.result }; }