import { useEffect, useRef, useState } from 'react'; import { tr } from './i18n'; /** Minutes left from recent samples of "GB done": the rate over the last minute and a half, so a stall or a burst does not * swing the estimate. Null until there is enough to say (about 10 s of samples) or when nothing is moving. */ export function etaMinutes(samples: { at: number; done: number }[], remainingGb: number | undefined): number | null { if (!Number.isFinite(remainingGb) || samples.length < 2) return null; const first = samples[0], last = samples[samples.length - 1]; const seconds = (last.at - first.at) / 1000, gained = last.done - first.done; if (seconds < 10 || gained <= 0) return null; return Math.max(1, Math.ceil((remainingGb! / (gained / seconds)) / 60)); } export function etaLabel(minutes: number | null): string { if (minutes === null) return ''; if (minutes >= 90) { const h = Math.floor(minutes / 60), m = minutes % 60; return m ? tr('About {0} h {1} min left', [h, m]) : tr('About {0} h left', [h]); } return tr('About {0} min left', [minutes]); } /** The approximate time left of a running download, for the progress bar. `key` restarts the estimate for another job. */ export function useDownloadEta(download: { done_gb?: number; remaining_gb?: number } | undefined, key: string | undefined): string { const samples = useRef<{ at: number; done: number }[]>([]); const owner = useRef(undefined); const [label, setLabel] = useState(''); const done = download?.done_gb, remaining = download?.remaining_gb; useEffect(() => { if (owner.current !== key) { owner.current = key; samples.current = []; } if (!Number.isFinite(done)) { setLabel(''); return; } const now = Date.now(); const list = samples.current; if (!list.length || list[list.length - 1].done !== done) list.push({ at: now, done: done! }); while (list.length > 2 && now - list[0].at > 90_000) list.shift(); setLabel(etaLabel(etaMinutes(list, remaining))); }, [done, remaining, key]); return label; } /** What the progress UI reads from an install job's `install` object: the parts it is downloading (the model's files and, * when the job adds it, the engine) summed into ONE count. A part is {kind, name, done_gb, total_gb, state}. Without `parts` * the job's combined `download` fields ({done_gb, total_gb, remaining_gb, percent}) are used as they are. */ export interface InstallParts { parts?: { done_gb?: number; total_gb?: number; state?: string }[]; download?: { done_gb?: number; total_gb?: number; remaining_gb?: number; percent?: number; file?: string } } export function installProgress(install: InstallParts | undefined): { done_gb: number; total_gb: number; remaining_gb: number; percent: number; file?: string } | undefined { /* setup is checking files that are all on disk (strata_mcp combined_progress: file "checking files"): the check's own figure, not the parts' (which stood at 96% for the whole check, DL99) */ const check = install?.download; if (check?.file === 'checking files' && Number.isFinite(check.done_gb) && Number.isFinite(check.total_gb) && (check.total_gb ?? 0) > 0) return { done_gb: check.done_gb!, total_gb: check.total_gb!, remaining_gb: Math.max(0, check.total_gb! - check.done_gb!), percent: Math.min(100, Math.max(0, check.percent ?? Math.floor(100 * check.done_gb! / check.total_gb!))), file: check.file }; const parts =(install?.parts ?? []).filter(p => Number.isFinite(p.total_gb) && (p.total_gb ?? 0) > 0 && p.state !== 'skipped' && p.state !== 'current'); if (parts.length) { const total = parts.reduce((n, p) => n + (p.total_gb ?? 0), 0); const done = Math.min(total, parts.reduce((n, p) => n + Math.min(p.done_gb ?? 0, p.total_gb ?? 0), 0)); // 100 once every part is whole (the engine's too); 99 at most while one is still coming const whole = parts.every(p => p.state === 'done'); return { done_gb: done, total_gb: total, remaining_gb: Math.max(0, total - done), percent: whole ? 100 : Math.min(99, Math.floor(100 * done / total)), file: install?.download?.file }; } const d = install?.download; if (d && Number.isFinite(d.done_gb) && Number.isFinite(d.total_gb)) return { done_gb: d.done_gb!, total_gb: d.total_gb!, remaining_gb: Number.isFinite(d.remaining_gb) ? d.remaining_gb! : Math.max(0, d.total_gb! - d.done_gb!), percent: d.percent ?? 0, file: d.file }; return undefined; } /** Which part of a multi-part install is being fetched now: the model's parts (with "part 2 of 4"), the picture reader or the engine. */ export interface InstallStage { kind: 'model' | 'vision' | 'engine' | 'draft' | 'other'; index?: number; count?: number } export function installStage(install: { parts?: { kind?: string; state?: string; files_done?: number; files_total?: number }[]; running?: boolean } | undefined): InstallStage | undefined { const parts = install?.parts ?? []; const now = parts.find(p => p.state === 'downloading') ?? parts.find(p => !['done', 'current', 'skipped', 'failed'].includes(p.state ?? '')); if (!now) return undefined; const kind = now.kind === 'model' || now.kind === 'vision' || now.kind === 'engine' || now.kind === 'draft' ? now.kind : 'other'; const count = Number(now.files_total) > 1 ? Number(now.files_total) : undefined; return { kind, ...(count ? { index: Math.min(count, Number(now.files_done ?? 0) + 1), count } : {}) }; } /** While a download server has not answered yet (setup's waiting phase, strata_control/netpolicy.py): setup's own line, translated (net-.json); undefined otherwise. */ export function waitingText(install: { phase?: string; label?: string } | undefined): string | undefined { return install?.phase === 'waiting' && install.label ? tr(install.label) : undefined; } /** The sentence for it, in plain words. */ export function stageText(stage: InstallStage | undefined): string { if (!stage) return tr('Downloading model files…'); if (stage.kind === 'vision') return tr('Downloading the picture reader…'); if (stage.kind === 'draft') return tr('Preparing your model…'); // the speed-up layer's own download (setup step 6) if (stage.kind === 'engine') return tr('Downloading the engine for this PC…'); if (stage.count && stage.index) return tr('Downloading model files · part {0} of {1}', [stage.index, stage.count]); return tr('Downloading model files…'); } /** The setup wizard's status line: a server that has not answered yet, else setup's own sentence for what it does now (checking the files, * preparing the tokenizer, ...; strata_control/progress_words.py, the line the Models page shows), else the part being fetched. Without it the * wizard said "Downloading the picture reader…" while the model file was checked and "Downloading model files…" after the last file (DL99). */ export function guidedStatusText(install: { phase?: string; label?: string; parts?: { kind?: string; state?: string; files_done?: number; files_total?: number }[] } | undefined, progress: unknown): string { return waitingText(install) ?? install?.label ?? (progress ? stageText(installStage(install)) : 'Preparing your model…'); }