import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { listArticleDirs, listPendingArticles } from "yt-dlp-transcript-common/lib/report/registry"; import { readGenerationUsage } from "yt-dlp-transcript-common/controller/generationUsage"; import { readPolicy, readToday, type PolicyView } from "@/lib/articles/generation"; import { ARTICLES_KEY } from "@/lib/articles/key.mjs"; import { sitesPaths } from "@/lib/articles/sites"; import { spendHistory } from "@/lib/create/estimate.mjs"; import { readPieceGraph } from "@/lib/pieces"; import { readSchedules } from "@/lib/schedules"; import { projectRefs } from "@/lib/projects"; import { makeKinds, sweepReports } from "@/lib/projects/create.mjs"; import { listCases } from "@/lib/research/boards"; import { listSources } from "@/lib/research/sources"; import type { CaseSummary } from "@/lib/research/case-types"; import type { SourceOption } from "@/lib/research/types"; // --------------------------------------------------------------------------- // What Create (/create, plans/umtool-redesign.md P5) needs to draw its lines // and open their forms: the policy (the chip, the new-article form), the day's // spend and the ledger's history (the estimate), the article ids taken, the // boards, the store's articles (a video from one), the sweep reports on disk, // the corpora a case may read, and the kinds the scaffolds write. // // Every part is read on its own and fails on its own: an editor that does not // answer costs the policy and the channels, never the page. // --------------------------------------------------------------------------- export type Today = { day: string; usd: number; jobs: number; daily: number }; export type ScaffoldKind = { id: string; label: string; scaffold: { fields: string[]; brands?: { id: string; label: string }[] } }; export type StoreArticle = { id: string; title: string; status: string | null }; export type SweepReport = { project: string; path: string; title: string }; export type CreateData = { policy: PolicyView | null; policyError: string | null; today: Today | null; /** task -> model -> per-job USD, oldest first (lib/create/estimate.mjs). */ history: Record>; taken: string[]; cases: CaseSummary[]; casesError: string | null; articles: StoreArticle[]; reports: SweepReport[]; sources: SourceOption[]; kinds: { recording: ScaffoldKind | null; report: ScaffoldKind | null; song: ScaffoldKind | null }; /** How many recurring reports are scheduled (R5), for the line's words. */ schedules: number; }; const msg = (e: unknown) => (e instanceof Error ? e.message : String(e)); export async function readCreate(): Promise { let policy: PolicyView | null = null; let policyError: string | null = null; try { policy = await readPolicy(); } catch (e) { policyError = msg(e); } const paths = sitesPaths(); const [today, rows, taken, cases, graph, refs, sources] = await Promise.all([ policy ? readToday(policy.generation.caps.daily).catch(() => null) : Promise.resolve(null), readGenerationUsage(getPaths().generationUsageFile).catch(() => []), Promise.all([listArticleDirs(paths), listPendingArticles(paths)]).then(([a, b]) => [...a, ...b]).catch(() => [] as string[]), listCases().then( (c) => ({ cases: c, error: null as string | null }), (e) => ({ cases: [] as CaseSummary[], error: msg(e) }), ), readPieceGraph().catch(() => null), projectRefs().catch(() => []), listSources().catch(() => [] as SourceOption[]), ]); const articles: StoreArticle[] = (graph?.pieces ?? []) .filter((p) => p.kind === "article" && p.site === ARTICLES_KEY && p.report) .sort((a, b) => b.newestMs - a.newestMs) .map((p) => ({ id: p.report as string, title: p.title, status: p.status })); return { policy, policyError, today, history: spendHistory(rows), taken, cases: cases.cases.filter((c) => !c.error), casesError: cases.error, articles, reports: await sweepReports(refs).catch(() => []), sources, kinds: makeKinds() as CreateData["kinds"], schedules: (await readSchedules().catch(() => ({ schedules: [] }))).schedules.length, }; }