import type { Job, JobStage } from "@/lib/types"; /** * Per stage share of a whole render. A bar that resets on every stage boundary * reads as a fault, so the UI shows one number that only moves forward. */ const WEIGHTS: Record = { transcribing: 12, planning: 18, conditioning: 15, rendering: 45, decoding: 10, }; function stagesFor(job: Job): JobStage[] { const stages: JobStage[] = []; if (job.request.scoreSource === "reference") stages.push("transcribing"); if (job.request.scoreSource === "model") stages.push("planning"); stages.push("conditioning", "rendering", "decoding"); return stages; } /** 0 to 1 across the whole job, not just the current stage. */ export function overallProgress(job: Job): number { if (job.stage === "done") return 1; if (job.stage === "queued") return 0; if (job.stage === "failed" || job.stage === "cancelled") return 0; const stages = stagesFor(job); const total = stages.reduce((sum, stage) => sum + (WEIGHTS[stage] ?? 0), 0); const index = stages.indexOf(job.stage); if (index < 0 || total === 0) return 0; const before = stages.slice(0, index).reduce((sum, stage) => sum + (WEIGHTS[stage] ?? 0), 0); const current = (WEIGHTS[job.stage] ?? 0) * job.stageProgress; return Math.min(1, (before + current) / total); } /** Null when stages come from polling, since a percentage would be invented. */ export function progressPercent(job: Job): number | null { if (!job.liveProgress) return null; return Math.round(overallProgress(job) * 100); }