File size: 1,548 Bytes
bf68acd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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<string, number> = {
  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);
}