okami.mom/strata

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

Niko1221Merge main (87dfca0e7) into f-own-int: the 0.2.14 app branch, fixtures and CI fixes, the Qwen4-readiness tools, the shape-engine build scriptsffb3d9b31

main
10.3 KiB132 linesraw
1import { OFFLINE_MESSAGE, looksOffline, plainUpdateError } from './update-messages.mjs';
2
3/** Read-only release checks. Installing/rolling back needs a managed installer. */
4const RELEASES = 'https://api.github.com/repos/Niko1221/Strata/releases?per_page=30';
5const RELEASE_PREFIX = 'https://github.com/Niko1221/Strata/releases/';
6
7function versionParts(value) {
8  const match = /^v?(\d+)\.(\d+)\.(\d+)(?:\+[\w.-]+)?$/.exec(value ?? '');
9  return match ? match.slice(1).map(Number) : null;
10}
11function compare(a, b) {
12  for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1;
13  return 0;
14}
15
16export async function checkUpdates({ appVersion, engineVersion }, fetcher = fetch) {
17  let strata;
18  try {
19    const response = await fetcher(RELEASES, { headers: { accept: 'application/vnd.github+json' }, signal: AbortSignal.timeout(10000) });
20    if (!response.ok) throw new Error(response.status === 403 || response.status === 429 ? 'The release service is busy. Try again later.' : 'Could not reach the Strata release feed. Try again.');
21    const releases = await response.json();
22    if (!Array.isArray(releases)) throw new Error('The release service returned an invalid response.');
23    const candidates = releases.filter(r => !r.draft && !r.prerelease && versionParts(r.tag_name));
24    candidates.sort((a, b) => compare(versionParts(b.tag_name), versionParts(a.tag_name)));
25    const latest = candidates[0];
26    const installed = versionParts(appVersion);
27    if (!latest) strata = { status: 'no-releases', message: 'No stable Strata release is published in this feed yet.' };
28    else if (!installed) strata = { status: 'unavailable', message: 'This installation has no comparable version number.' };
29    else {
30      const newer = compare(versionParts(latest.tag_name), installed) > 0;
31      // Release content is untrusted. Only link back to the known repository.
32      const releaseUrl = typeof latest.html_url === 'string' && latest.html_url.startsWith(RELEASE_PREFIX) ? latest.html_url : undefined;
33      strata = { status: newer ? 'available' : 'current', latestVersion: latest.tag_name, releaseUrl, message: newer ? `Strata ${latest.tag_name} is available.` : 'Strata is up to date.' };
34    }
35  } catch (error) {
36    strata = { status: 'error', message: looksOffline(error) ? OFFLINE_MESSAGE : error.name === 'TimeoutError' ? 'The release check timed out. Try again.' : error.message ?? 'Could not check for updates.' };
37  }
38  return {
39    checkedAt: new Date().toISOString(),
40    strata: { installedVersion: appVersion, ...strata, canInstall: false },
41    // The existing binary downloader has no tested-release compatibility feed
42    // or trustworthy installed build ID. Upstream's latest is not an update verdict.
43    engine: { installedVersion: engineVersion, status: 'unavailable', canInstall: false, message: 'Engine update checks are not available in this build.' },
44  };
45}
46
47/** The engine row after a version failed its test start on this computer (owner-approved wording, 11-09). */
48export const engineFailedBeforeText = (offered, installed) => `Engine ${offered ?? 'update'} didn't start correctly on this computer, so Strata kept engine ${installed ?? 'in use'}. Press Update engine to try again.`;
49
50/**
51 * The packaged app's update check: the signed feed, read by Control (`strata update --check`), plus the shell's own verdict
52 * on the app. Same result shape as `checkUpdates`, so the Settings screen needs no change. This is the only place the host
53 * reaches the feed, and only when the user presses "Check for updates".
54 */
55export async function checkComponentUpdates({ control, appUpdater = null, appVersion = null }) {
56  const checkedAt = new Date().toISOString();
57  let report;
58  try {
59    report = await control.updateCheck({ appVersion });
60  } catch (error) {
61    const plain = plainUpdateError(error, 'Strata');
62    const message = /update server|aren't available|No internet/.test(plain.headline) ? plain.message : `Couldn't check for updates. Try again in a moment.`;
63    return { checkedAt, strata: { installedVersion: appVersion ?? control.version ?? null, status: 'error', message, details: plain.details, canInstall: false }, engine: { installedVersion: null, status: 'error', message, details: plain.details, canInstall: false } };
64  }
65  const mib = bytes => `${Math.round((bytes ?? 0) / 1_000_000)} MB`;      // decimal megabytes, like the Terminal and the release page
66  const engine = report.engine ?? {};
67  const installedEngine = engine.installed?.version ?? null;
68  const offered = engine.offered?.version ?? null;
69  const engineView = { installedVersion: installedEngine, latestVersion: offered ?? undefined, canInstall: engine.status === 'available' || engine.status === 'install' };
70  if (engine.status === 'available' && engine.installed_locally) Object.assign(engineView, { status: 'available', message: 'A newer engine is already on this computer. Update switches to it; nothing is downloaded.' });
71  else if (engine.status === 'available') Object.assign(engineView, { status: 'available', message: `A new engine is available (${mib(engine.download_bytes)}).` });
72  else if (engine.status === 'install') Object.assign(engineView, { status: 'available', message: `This PC needs an engine to run models. Press Update engine to download it (${mib(engine.download_bytes)}).` });
73  // A version that failed its test start here is held back by Control for a day (status failed-before). It is not 'up to date': the row says what happened and
74  // offers the update again (the app's Update engine passes --force, so the press really tries it again). QAGUI-LIN 0213.
75  else if (engine.status === 'failed-before') Object.assign(engineView, { status: 'available', canInstall: true, retry: true, message: engineFailedBeforeText(offered, installedEngine), details: engine.note });
76  else if (engine.status === 'current' || engine.status === 'skipped') Object.assign(engineView, { status: 'current', message: 'The engine is up to date.', details: engine.note });
77  else if (engine.status === 'wrong-make') Object.assign(engineView, { status: 'unavailable', message: engine.note ?? 'Nothing was changed: the update was for another make of graphics card.', details: (engine.reasons ?? []).join('\n') || undefined });
78  else if (engine.status === 'not-in-feed') Object.assign(engineView, { status: 'current', message: 'No engine update is listed right now.' });
79  else Object.assign(engineView, { status: 'unavailable', message: "There isn't an engine for this PC's graphics card yet.", details: (engine.reasons ?? []).join('\n') || undefined });
80  const control_ = report.control ?? {};
81  const app = report.app ?? {};
82  let appNew = app.status === 'available' ? app.offered : null;
83  let appNotes = app.notes ?? '';
84  let appCheckFailed = null;   // the shell's own (Tauri) check threw: before this, the Settings check hid it and showed only Control's text
85  let appCannot = null;   // browser mode: the app window's updater is not running, so the app itself cannot be updated from here
86  if (appUpdater) {
87    try {
88      const verdict = await appUpdater.check();
89      appNew = verdict.available ? verdict.version : null;
90      appNotes = verdict.notes ?? appNotes;
91    } catch (error) {
92      if (error?.code === 'app-update-unavailable' && appUpdater.unavailable) appCannot = appUpdater.unavailable;
93      else appCheckFailed = String(error?.message ?? error ?? 'The app update check failed.');   // the app's own check did not work: say so, raw text behind Details
94    }
95  }
96  const controlNew = control_.status === 'available' ? control_.offered : null;
97  const strata = { installedVersion: appVersion ?? control_.installed ?? control.version ?? null, canInstall: !!((appNew && !appCannot) || controlNew) };
98  const appCheckLine = "Strata couldn't check for a new version of the app itself. Try again later.";
99  if (appNew || controlNew) Object.assign(strata, { status: 'available', latestVersion: appNew ?? controlNew, message: `A new version of Strata is available.${appNotes ? ` ${appNotes}` : ''}${appNew && appCannot ? ` ${appCannot}` : ''}${appCheckFailed ? ` ${appCheckLine}` : ''}` });
100  else if (appCheckFailed) Object.assign(strata, { status: 'error', message: appCheckLine });
101  else Object.assign(strata, { status: 'current', message: 'Strata is up to date.' });
102  if (appCheckFailed) strata.details = appCheckFailed;
103  return { checkedAt, feed: report.feed, strata, engine: engineView, serving: report.serving ?? [] };
104}
105
106/**
107 * What the first-run card shows: this PC's graphics card(s), the engine package the signed feed offers for it and how big it is.
108 * One feed read, nothing downloaded. `status`: offer (a package fits), none (nothing fits; `reasons` say why), offline (the feed could not be
109 * read), installed (an engine is already here), wrong-make (an engine for another make of card is installed).
110 */
111export async function engineFirstRun({ control, gpus = [] }) {
112  const cards = gpus.map(g => ({ name: g.name ?? 'Graphics card', vramGb: g.vramGb ?? null }));
113  let report;
114  try {
115    report = await control.updateCheck({});
116  } catch (error) {
117    const plain = plainUpdateError(error, 'Strata');
118    return { status: looksOffline(error) || /update server|No internet/.test(plain.headline ?? '') ? 'offline' : 'error', gpus: cards, message: plain.message, details: plain.details };
119  }
120  const engine = report.engine ?? {};
121  const hardware = report.hardware ?? {};
122  const base = { gpus: cards, driver: hardware.nvidia_driver ?? null, backend: hardware.backend ?? null };
123  if (engine.installed) return { ...base, status: 'installed', installedVersion: engine.installed.version ?? null };
124  if (engine.status === 'wrong-make') return { ...base, status: 'wrong-make', message: engine.note ?? null, reasons: engine.reasons ?? [] };
125  if (engine.offered && (engine.status === 'install' || engine.status === 'available')) {
126    // A newer Strata Control in the same feed comes with the engine (an Arc user who has just installed the app needed 0.1.59.1's fix and had to find Settings > Updates; ARCFIX 0213).
127    const ctl = report.control ?? {};
128    const control = ctl.status === 'available' && ctl.offered ? { version: ctl.offered, bytes: ctl.download_bytes || 0 } : null;
129    return { ...base, status: 'offer', offer: { version: engine.offered.version, bytes: engine.download_bytes || engine.offered.size || 0, variant: engine.offered.variant ?? '', backend: engine.offered.backend ?? null }, control };
130  }
131  return { ...base, status: 'none', message: engine.note ?? null, reasons: engine.reasons ?? [] };
132}