okami.mom/strata

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

Niko1221Merge f-ds-out ac306afb2 into f-own-intaff3a132f

main
4.2 KiB78 linesraw
1import type { NativeState } from './native';
2import { nativeCatalogModels } from './native-catalog';
3
4/** The parts of Settings > Engine that need no React: the list's shape, sizes, makes, version order, and which models need a newer engine. */
5/** One engine package installed on this computer, as Control lists it (`strata components engines`, strata_control/engine_library.py). */
6/** qwen, or the engine of a Qwen4 model shape: shape:<shape> (strata_control/engine_library.py SHAPE_PREFIX; an id, never shown). */
7export type EngineFamily = 'qwen' | `shape:${string}`;
8export interface InstalledEngine {
9  family: EngineFamily;
10  folder: string;
11  version: string;
12  backend: string;
13  platform: string;
14  variant: string;
15  bytes: number;
16  /** In use: the one a model starts on. */
17  active: boolean;
18  /** The newest one that can run on this computer. */
19  newest: boolean;
20  fits: boolean;
21  /** Why this computer cannot run it: a code the page words, and its values. */
22  why_code?: string | null;
23  why_args?: string[];
24  can_use: boolean;
25  can_delete: boolean;
26  blocked_code?: 'active' | 'running' | null;
27  build_id?: string;
28  path?: string;
29  root?: string;
30}
31export interface EngineGroup { family: EngineFamily; /** a model shape's engine: its shape (q4_lite) */ shape?: string; /** null for a shape: the page names it (engineGroupTitle) */ title: string | null; engines: InstalledEngine[] }
32
33/** The model family of the feed that runs on a shape's engine (strata_control/shape_engine.py family_of). */
34const SHAPE_FAMILY_PREFIX = 'qwen4exp_';
35/**
36 * The heading of a group in Settings > Engine: Control's title for the Qwen engine; for a model shape's engine the model's own title from the
37 * catalog, the shape's name when the catalog does not have it. No new words; the family id is never shown.
38 */
39export function engineGroupTitle(group: EngineGroup, catalogFamilies: readonly { family: string; title?: string | null }[] | null | undefined): string {
40  if (group.title) return group.title;
41  const shape = group.shape ?? (group.family.startsWith('shape:') ? group.family.slice(6) : '');
42  const title = (catalogFamilies ?? []).find(f => f.family === SHAPE_FAMILY_PREFIX + shape)?.title;
43  return title?.trim() || shape;
44}
45export interface EngineList { available: boolean; families: EngineGroup[]; total_bytes: number }
46
47/** The make of graphics card an engine is built for, in the words a person knows. */
48export const engineMake = (backend: string): string => ({ cuda: 'NVIDIA', hip: 'AMD', sycl: 'Intel', metal: 'Apple', cpu: 'CPU' } as Record<string, string>)[backend] ?? backend;
49
50/** "1.0 GB", or "640 MB" below a gigabyte: decimal units, like the Terminal and the release page. */
51export function engineSize(bytes: number, locale?: string): string {
52  if (bytes >= 1e9) return `${(bytes / 1e9).toLocaleString(locale, { minimumFractionDigits: 1, maximumFractionDigits: 1 })} GB`;
53  return `${Math.max(1, Math.round(bytes / 1e6)).toLocaleString(locale)} MB`;
54}
55
56const parts = (v: string) => v.split(/[.\-+]/).map(p => (/^\d+$/.test(p) ? Number(p) : 0));
57/** Is version a older than version b? ("0.1.9" < "0.1.10", "0.1.49" < "0.1.49.1"). */
58export function versionLess(a: string, b: string): boolean {
59  const x = parts(a), y = parts(b);
60  for (let i = 0; i < Math.max(x.length, y.length); i++) if ((x[i] ?? 0) !== (y[i] ?? 0)) return (x[i] ?? 0) < (y[i] ?? 0);
61  return false;
62}
63
64/**
65 * The installed models that need a newer engine than `version` (the catalog's `engine_min`). A switch to an older engine warns about these and goes on:
66 * Strata recommends, it does not force. Only the Qwen family has catalog minimums.
67 */
68export function modelsNeedingNewer(native: NativeState | null | undefined, engine: InstalledEngine): string[] {
69  if (!native?.installed?.models.length || !native.catalog || engine.family !== 'qwen') return [];
70  const sizes = nativeCatalogModels(native.catalog.families).flatMap(f => f.sizes) as unknown as { id: string; model: string; engine_min?: string | null }[];
71  const names: string[] = [];
72  for (const installed of native.installed.models) {
73    const size = sizes.find(s => s.id === installed.model);
74    if (size?.engine_min && versionLess(engine.version, size.engine_min)) names.push(installed.model_name || size.model || installed.model);
75  }
76  return names;
77}
78