okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-dl99-fixes: the Qwen3.6 first-run download (no engine-race stop, no cancelled engine download, progress to 100%, the picture reader's real figure, the engine-not-ready sentence)361c8d94a

main
7.1 KiB93 linesraw
1import { useEffect, useRef, useState } from 'react';
2import { tr } from './i18n';
3
4/** 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
5 *  swing the estimate. Null until there is enough to say (about 10 s of samples) or when nothing is moving. */
6export function etaMinutes(samples: { at: number; done: number }[], remainingGb: number | undefined): number | null {
7  if (!Number.isFinite(remainingGb) || samples.length < 2) return null;
8  const first = samples[0], last = samples[samples.length - 1];
9  const seconds = (last.at - first.at) / 1000, gained = last.done - first.done;
10  if (seconds < 10 || gained <= 0) return null;
11  return Math.max(1, Math.ceil((remainingGb! / (gained / seconds)) / 60));
12}
13
14export function etaLabel(minutes: number | null): string {
15  if (minutes === null) return '';
16  if (minutes >= 90) {
17    const h = Math.floor(minutes / 60), m = minutes % 60;
18    return m ? tr('About {0} h {1} min left', [h, m]) : tr('About {0} h left', [h]);
19  }
20  return tr('About {0} min left', [minutes]);
21}
22
23/** The approximate time left of a running download, for the progress bar. `key` restarts the estimate for another job. */
24export function useDownloadEta(download: { done_gb?: number; remaining_gb?: number } | undefined, key: string | undefined): string {
25  const samples = useRef<{ at: number; done: number }[]>([]);
26  const owner = useRef<string | undefined>(undefined);
27  const [label, setLabel] = useState('');
28  const done = download?.done_gb, remaining = download?.remaining_gb;
29  useEffect(() => {
30    if (owner.current !== key) { owner.current = key; samples.current = []; }
31    if (!Number.isFinite(done)) { setLabel(''); return; }
32    const now = Date.now();
33    const list = samples.current;
34    if (!list.length || list[list.length - 1].done !== done) list.push({ at: now, done: done! });
35    while (list.length > 2 && now - list[0].at > 90_000) list.shift();
36    setLabel(etaLabel(etaMinutes(list, remaining)));
37  }, [done, remaining, key]);
38  return label;
39}
40
41/** What the progress UI reads from an install job's `install` object: the parts it is downloading (the model's files and,
42 *  when the job adds it, the engine) summed into ONE count. A part is {kind, name, done_gb, total_gb, state}. Without `parts`
43 *  the job's combined `download` fields ({done_gb, total_gb, remaining_gb, percent}) are used as they are. */
44export 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 } }
45export function installProgress(install: InstallParts | undefined): { done_gb: number; total_gb: number; remaining_gb: number; percent: number; file?: string } | undefined {
46  /* setup is checking files that are all on disk (strata_mcp combined_progress: file "checking files"): the check's own figure, not the
47     parts' (which stood at 96% for the whole check, DL99) */
48  const check = install?.download;
49  if (check?.file === 'checking files' && Number.isFinite(check.done_gb) && Number.isFinite(check.total_gb) && (check.total_gb ?? 0) > 0)
50    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 };
51  const parts =(install?.parts ?? []).filter(p => Number.isFinite(p.total_gb) && (p.total_gb ?? 0) > 0 && p.state !== 'skipped' && p.state !== 'current');
52  if (parts.length) {
53    const total = parts.reduce((n, p) => n + (p.total_gb ?? 0), 0);
54    const done = Math.min(total, parts.reduce((n, p) => n + Math.min(p.done_gb ?? 0, p.total_gb ?? 0), 0));
55    // 100 once every part is whole (the engine's too); 99 at most while one is still coming
56    const whole = parts.every(p => p.state === 'done');
57    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 };
58  }
59  const d = install?.download;
60  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 };
61  return undefined;
62}
63
64/** 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. */
65export interface InstallStage { kind: 'model' | 'vision' | 'engine' | 'draft' | 'other'; index?: number; count?: number }
66export function installStage(install: { parts?: { kind?: string; state?: string; files_done?: number; files_total?: number }[]; running?: boolean } | undefined): InstallStage | undefined {
67  const parts = install?.parts ?? [];
68  const now = parts.find(p => p.state === 'downloading') ?? parts.find(p => !['done', 'current', 'skipped', 'failed'].includes(p.state ?? ''));
69  if (!now) return undefined;
70  const kind = now.kind === 'model' || now.kind === 'vision' || now.kind === 'engine' || now.kind === 'draft' ? now.kind : 'other';
71  const count = Number(now.files_total) > 1 ? Number(now.files_total) : undefined;
72  return { kind, ...(count ? { index: Math.min(count, Number(now.files_done ?? 0) + 1), count } : {}) };
73}
74/** While a download server has not answered yet (setup's waiting phase, strata_control/netpolicy.py): setup's own line, translated (net-<lang>.json); undefined otherwise. */
75export function waitingText(install: { phase?: string; label?: string } | undefined): string | undefined {
76  return install?.phase === 'waiting' && install.label ? tr(install.label) : undefined;
77}
78/** The sentence for it, in plain words. */
79export function stageText(stage: InstallStage | undefined): string {
80  if (!stage) return tr('Downloading model files…');
81  if (stage.kind === 'vision') return tr('Downloading the picture reader…');
82  if (stage.kind === 'draft') return tr('Preparing your model…');   // the speed-up layer's own download (setup step 6)
83  if (stage.kind === 'engine') return tr('Downloading the engine for this PC…');
84  if (stage.count && stage.index) return tr('Downloading model files · part {0} of {1}', [stage.index, stage.count]);
85  return tr('Downloading model files…');
86}
87/** 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,
88 *  preparing the tokenizer, ...; strata_control/progress_words.py, the line the Models page shows), else the part being fetched. Without it the
89 *  wizard said "Downloading the picture reader…" while the model file was checked and "Downloading model files…" after the last file (DL99). */
90export function guidedStatusText(install: { phase?: string; label?: string; parts?: { kind?: string; state?: string; files_done?: number; files_total?: number }[] } | undefined,
91                                 progress: unknown): string {
92  return waitingText(install) ?? install?.label ?? (progress ? stageText(installStage(install)) : 'Preparing your model…');
93}