import { tr } from '../lib/i18n'; import { useCallback, useEffect, useRef, useState } from 'react'; import { Brand } from './Brand'; import { Bar, Btn, Icon } from './ui'; import { api } from '../lib/client'; import { useDownloadEta } from '../lib/download-eta'; import type { NativeJob } from '../lib/native'; import { megabytes } from '../lib/format'; import { FirstRunLanguage } from './FirstRunLanguage'; /** What the host says about this PC's engine (GET /api/engine/first-run). */ export interface FirstRunInfo { status: 'offer' | 'none' | 'offline' | 'error' | 'installed' | 'wrong-make' | 'unavailable'; gpus: { name: string; vramGb: number | null }[]; driver?: string | null; offer?: { version: string; bytes: number; variant: string; backend: string | null }; /** A newer Strata Control in the same feed: the Download takes it too (the Arc fix of 0.2.13.1 came this way). */ control?: { version: string; bytes: number } | null; message?: string | null; reasons?: string[]; details?: string; freeBytes?: number; } export const mb = megabytes; /** After the size when the download also brings the newer Strata Control (owner-approved wording, 11-09). */ export const WITH_CONTROL = " (with the Strata setup update)"; /** * Whether this PC still needs its engine. Reads only Control's local answer (no network): the engine is missing and the engine that came * with the app is not being unpacked. `null` while that is not known yet. */ export function useEngineNeeded(): boolean | null { const [needed, setNeeded] = useState(null); useEffect(() => { let live = true; const ask = () => api.get<{ engine?: { usable?: boolean } | null; engineSetup?: boolean }>('/api/control/status').then(s => { if (!live) return; if (s.engineSetup) return setNeeded(null); setNeeded(!!s.engine && s.engine.usable === false); }, () => { if (live) setNeeded(false); }); void ask(); const timer = setInterval(() => void ask(), 3000); return () => { live = false; clearInterval(timer); }; }, []); return needed; } function gpuLine(info: FirstRunInfo) { // a processor's own tiny graphics (no memory of its own) is not "a card" const first = info.gpus.find(g => (g.vramGb ?? 0) >= 1) ?? info.gpus[0]; if (!first) return ''; const vram = first.vramGb ? `, ${Math.round(first.vramGb)} GB` : ''; const driver = info.driver ? `, driver ${info.driver}` : ''; return `${first.name}${vram}${driver}`; } /** * The first-run card: the engine this PC needs, one button to download it, progress, and the offline way (choose a downloaded file). * Nothing is downloaded before a button is pressed. */ export function EngineFirstRun({ onDone }: { onDone: () => void }) { const [info, setInfo] = useState(null); const [job, setJob] = useState(null); const [busy, setBusy] = useState(false), [problem, setProblem] = useState(''); const [finished, setFinished] = useState(false); const file = useRef(null); const started = useRef(false); const look = useCallback(() => { setInfo(null); setProblem(''); api.get('/api/engine/first-run').then(setInfo, e => setInfo({ status: 'error', gpus: [], message: e instanceof Error ? e.message : String(e) })); }, []); useEffect(() => { look(); }, [look]); // follow the download / install job useEffect(() => { const timer = setInterval(() => { if (!started.current) return; api.get<{ job: NativeJob | null }>('/api/engine/job').then(r => { setJob(r.job); if (r.job?.state === 'done') setFinished(true); if (r.job && r.job.state !== 'running') started.current = false; }, () => { }); }, 700); return () => clearInterval(timer); }, []); async function start() { setBusy(true); setProblem(''); try { started.current = true; setJob(await api.post('/api/native/operations', { kind: 'update-engine', args: { firstRun: true, ...(info?.control ? { withControl: true } : {}) } })); } catch (e) { started.current = false; setProblem(e instanceof Error ? e.message : String(e)); } finally { setBusy(false); } } async function chosen(files: FileList | null) { const picked = files?.[0]; if (!picked) return; setBusy(true); setProblem(''); try { const response = await fetch('/api/engine/from-file', { method: 'POST', headers: { 'x-file-name': picked.name }, body: picked }); const body = await response.json().catch(() => ({})); if (!response.ok) throw new Error(body?.error ?? `The file could not be used (${response.status}).`); started.current = true; setJob(body as NativeJob); } catch (e) { setProblem(e instanceof Error ? e.message : String(e)); } finally { setBusy(false); if (file.current) file.current.value = ''; } } const running = job?.state === 'running'; const failed = !!job && (job.state === 'error' || job.state === 'cancelled'); const percent = Number(job?.result?.install?.download?.percent ?? 0); const bytes = info?.offer?.bytes ?? 0; const doneMb = Math.round(mb(bytes) * percent / 100); const eta = useDownloadEta(running && bytes ? { done_gb: bytes * percent / 100 / 1e9, remaining_gb: bytes * (100 - percent) / 100 / 1e9 } : undefined, job?.id); const cards = (info?.gpus ?? []).filter(g => (g.vramGb ?? 0) >= 1).length; const hidden = void chosen(e.target.files)}/>; const pick = (label: string) => ; let body; if (finished) { body = <>

{tr("The engine is ready.")}

{tr("Next: pick a model.")}

{tr("Continue")}
; } else if (running) { body = <>

{tr("Downloading the engine")}

{bytes && job?.result?.install?.download ? `${doneMb} of ${mb(bytes)} MB${eta ? ' · ' + eta : ''}` : tr(job?.summary ?? 'Starting…')}

{tr("You can close Strata; it continues where it stopped.")}

; } else if (!info) { body =

{tr("Welcome to Strata")}

{tr("Looking at your graphics card…")}

; } else { const card = gpuLine(info); const found = card ?

{tr("Strata found your graphics card:")}
{card}{cards > 1 ? <>
{tr("{0} cards found. Strata uses the first one for the engine; change it later in Settings.", [cards])} : null}

:

{tr("Strata did not recognise a supported graphics card.")}

; let main, actions; if (info.status === 'offer' && info.offer) { main = <>{found}

{tr("To run models on it, Strata needs one download:")}
{tr("The engine for this card")}: {mb(info.offer.bytes + (info.control?.bytes ?? 0))} MB{info.control ? tr(WITH_CONTROL) : null}

{info.freeBytes !== undefined && info.freeBytes < info.offer.bytes * 2.5 ?

{tr("The engine needs about {0} GB of free space on this drive and only {1} GB is free. Free some space, then continue.", [Math.ceil(info.offer.bytes * 2.5 / 1e9), (info.freeBytes / 1e9).toFixed(1)])}

: null}

{tr("It comes from Strata’s own update server and is checked before it is used. Your models and chats are never touched by it.")}

; actions = <> void start()}>{tr(failed ? 'Continue' : 'Download the engine')}{pick("I already have the file…")}; } else if (info.status === 'offline') { main = <>{found}

{tr("Strata could not reach github.com. Check your connection and try again, or download the engine file on another PC and choose it here.")}

; actions = <>{tr("Try again")}{pick("Choose a downloaded file…")}; } else if (info.status === 'wrong-make') { main = <>{found}

{info.message ?? tr("The engine installed here is for another make of graphics card.")}

; actions = <>{tr("Try again")}{pick("Choose a downloaded file…")}; } else { main = <>{found}

{tr("Strata can’t pick an engine for this PC from its update list.")}

{info.message ?

{info.message}

: null}{(info.reasons ?? []).length ?
    {(info.reasons ?? []).map(r =>
  • {r}
  • )}
: null}; actions = <>{tr("Try again")}{pick("Choose a downloaded file…")}; } body = <>

{tr("Welcome to Strata")}

{main}
{failed ?

{job?.error ?? job?.summary}

{job?.details ?
{tr('Details')}
{job.details}
: null}

{percent >= 100 ? tr("The file was downloaded. Continue tries the install again.") : tr("The download stopped. Continue picks it up where it stopped.")}

: null} {problem ?

{problem}

: null}
{actions}
; } return
{hidden}
{body}
; }