import { CHANNELS_DIR, REPORTS_ROOT } from "./paths"; import { loadUsageIndex } from "./ingredients/usedIn.mjs"; import { readIngredient as readAt, STAGES as STAGE_LIST } from "./ingredients/stages.mjs"; // The app's view of lib/ingredients/ (lane L5, the ingredient life-cycle rail), typed and bound to the roots. // The readers, the file formats and the rules are there (plain ESM, unit-tested); `umtool ingredient` prints // the same object. SERVER ONLY. // // The usage index is memoised for a few seconds on globalThis (every bundle shares one copy, the rail-counts // lesson): a rebuild is a walk and a stat per manifest, and a page asks once per render. A write here clears it. export type PillKind = "ok" | "count" | "warn" | "none"; export type Pill = { kind: PillKind; n?: number }; export type UseRow = { project: string; clipId: string; start: number | null; end: number | null; cutStart: number | null; cutEnd: number | null; quote: string | null; inDraft: boolean; cites: string[]; versions: { id: string; kind: "version" | "rendered"; start: number | null; end: number | null; cutStart: number | null; cutEnd: number | null }[]; }; export type WindowRow = { name: string; file: string; from: number; to: number; bytes: number; mtime: string; desynced: boolean; focused: boolean; orphan: boolean; serves: { project: string; clipId: string }[]; partly: { project: string; clipId: string }[]; sidecar: { requestedBy: string | null; manifest: string | null; clipId: string | null; reason: string | null; fetchedAt: string | null; source: string | null; syncOk: boolean | null; clock: { id: string | null; offset: number | null; certain: boolean } | null; } | null; }; type Base = { key: string; label: string; pill: Pill; line: string }; export type Stage = Base & Record; export type FoundStage = Base & { findClips: { at: string | null; project: string; url: string | null; segments: number; segment: string | null; created: boolean } | null; marks: { session: string; query: string | null; state: string; at: string | null }[]; candidates: { case: string; key: string | null; from: number | null; to: number | null; title: string | null; hits: number }[]; }; export type DownloadedStage = Base & { meta: { bytes: number; id: string | null; title: string | null; webpageUrl: string | null; uploadDate: string | null; extractor: string | null } | null; history?: { count: number; lastAt: string | null; lastBy: string | null } | null; outcome?: { status: string; finishedAt: string | null; attempts: number; webpageUrl: string | null; error: string | null } | null; availability?: { availability: string; checkedAt: string | null } | null; saved?: { file: string; height: number | null; reachable: boolean } | null; imported?: { count: number; lastAt: string | null; via: string | null } | null; media: { name: string; link: boolean; reachable: boolean; bytes: number | null }[]; captions?: string[]; problems?: string[]; }; export type TranscribedStage = Base & { cues: { bytes: number; source: string | null; format: string | null; vttFile: string | null; engine: string | null; platform: string | null; title: string | null; uploadDate: string | null; duration: number | null; lastEnd: number | null; coverage: number | null } | null; pin?: Record | null; outcome?: { at: string | null; worker: string | null; durationMs: number | null; salvaged: boolean } | null; words?: { bytes: number } | null; captions?: string[]; }; export type ClockStage = Base & { clock: { id: string; ok: boolean; source: string | null; target: string | null; segments: number; steps: number; residualMs: number | null; measuredAt: string | null } | null; }; export type WindowsStage = Base & { windows: WindowRow[]; uncovered: { project: string; clipId: string; from: number | null; to: number | null }[]; savedWhole: boolean; /** The saved whole recording's path when its drive does not answer, else null. */ savedUnreachable: string | null; focus: { from: number; to: number; name: string; found: boolean } | null; }; export type UsedStage = Base & { rows: UseRow[] }; export type CheckedStage = Base & { clips: { project: string; clipId: string; hasQuote: boolean; status: string | null; offset: number | null; current: boolean; checkedAt: string | null }[] }; export type RenderedStage = Base & { renders: { project: string; clipId: string; version: string | null; kind: string; label: string | null; state: string | null; seconds: number | null; endedAt: string | null; cut: { from: number | null; to: number | null } | null }[]; }; export type PublishedStage = Base & { links: { project: string; clipId: string; url: string | null; state: string | null; fix: string | null; checkedAt: string | null }[] }; export type Ingredient = { ch: string; id: string; dirId: string; siteId: string | null; platform: string | null; exists: boolean; title: string | null; webpageUrl: string | null; duration: number | null; uploadDate: string | null; notes: string[]; focus: { from: number; to: number; name: string; found: boolean } | null; stages: [FoundStage, DownloadedStage, TranscribedStage, ClockStage, WindowsStage, UsedStage, CheckedStage, RenderedStage, PublishedStage]; }; export const STAGES = STAGE_LIST as { key: string; label: string }[]; const MEMO_MS = 5_000; const KEY = Symbol.for("umtool.usage-index"); type Memo = { at: number; value: Promise> } | null; const store = globalThis as unknown as Record; export function clearUsageIndex(): void { store[KEY] = null; } export function usageIndex(): Promise> { const m = store[KEY]; if (m && Date.now() - m.at < MEMO_MS) return m.value; const value = loadUsageIndex({ reportsRoot: REPORTS_ROOT, channelsDir: CHANNELS_DIR }).then((r) => r.index as Record); const entry = { at: Date.now(), value }; store[KEY] = entry; value.catch(() => { if (store[KEY] === entry) store[KEY] = null; }); return value; } /** One recording's nine stages (lib/ingredients/stages.mjs), or null for a channel or id that is not one. */ export async function readIngredient(ch: string, id: string, window: string | null = null): Promise { return (await readAt({ channelsDir: CHANNELS_DIR, reportsRoot: REPORTS_ROOT, ch, id, window, index: await usageIndex() })) as Ingredient; }