okami.mom/strata

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

Niko1221apps/host: the owner's UI and the Node App Host0b3299a01

main
3.9 KiB67 linesraw
1import type { ConfigDocument } from './config';
2import type { EngineConfig, Hardware, ImageModelChoice } from './types';
3
4export type ComputerUse = 'shared' | 'full';
5export interface RunProfile {
6  id: string;
7  name: string;
8  variantId: string;
9  hardware: string;
10  engineVersion: string;
11  config: ConfigDocument;
12  imageModel?: ImageModelChoice;
13  updatedAt: string;
14}
15export interface RunProfiles {
16  schema: 1;
17  profiles: RunProfile[];
18  defaults: Record<string, string>;
19}
20export const RUN_PROFILES_KEY = 'strata.run-profiles.v1';
21export const emptyProfiles = (): RunProfiles => ({ schema: 1, profiles: [], defaults: {} });
22
23export function computerIdentity(hardware: Hardware) {
24  return JSON.stringify({
25    host: hardware.host.name, platform: hardware.host.platform, arch: hardware.host.arch,
26    cpu: hardware.cpu.model, cores: hardware.cpu.cores, ram: hardware.memory.totalGb,
27    processors: hardware.processors?.map(p => [p.socket, p.model, p.cores]),
28    gpus: hardware.gpus.map(g => [g.id ?? g.index, g.name, g.vramGb, g.backend]),
29    backend: hardware.accelerator.backend,
30  });
31}
32export function defaultProfileKey(hardware: string, variantId: string) { return `${hardware}\n${variantId}`; }
33export function configIdentity(config: ConfigDocument): string {
34  const canonical = (v: unknown): unknown => Array.isArray(v) ? v.map(canonical) : v && typeof v === 'object'
35    ? Object.fromEntries(Object.entries(v).sort(([a], [b]) => a.localeCompare(b)).map(([k, item]) => [k, canonical(item)])) : v;
36  return JSON.stringify(canonical(config));
37}
38export function readProfiles(text: string | null): RunProfiles {
39  if (!text) return emptyProfiles();
40  const parsed = JSON.parse(text);
41  if (parsed?.schema !== 1 || !Array.isArray(parsed.profiles) || !parsed.defaults || typeof parsed.defaults !== 'object' || Array.isArray(parsed.defaults)) throw new Error('Saved run profiles could not be read.');
42  const profiles = parsed.profiles.filter((p: RunProfile) => p && typeof p.id === 'string' && typeof p.name === 'string' && typeof p.variantId === 'string' && typeof p.hardware === 'string' && typeof p.engineVersion === 'string' && p.config?.schema === 'strata.engine/1' && p.config.beginner && p.config.expert && !Array.isArray(p.config.expert)).map((p: RunProfile) => ({ ...p, imageModel: ['off','q4','q5','q6','q8'].includes(p.imageModel ?? '') ? p.imageModel : 'off' }));
43  const defaults = Object.fromEntries(Object.entries(parsed.defaults).filter(([, id]) => typeof id === 'string' && profiles.some((p: RunProfile) => p.id === id))) as Record<string, string>;
44  return { schema: 1, profiles, defaults };
45}
46export function saveProfile(store: RunProfiles, profile: RunProfile, makeDefault: boolean): RunProfiles {
47  const snapshot = structuredClone(profile);
48  const next = { ...store, profiles: [...store.profiles.filter(p => p.id !== profile.id), snapshot], defaults: { ...store.defaults } };
49  const key = defaultProfileKey(profile.hardware, profile.variantId);
50  if (makeDefault) next.defaults[key] = profile.id;
51  else if (next.defaults[key] === profile.id) delete next.defaults[key];
52  return next;
53}
54export function resourceConfig(config: ConfigDocument, use: ComputerUse, hardware: Hardware): ConfigDocument {
55  const cores = Math.max(1, hardware.cpu.cores);
56  return {
57    ...config, headroom: use,
58    expert: { ...config.expert, 'perf.threads': use === 'full' ? cores : Math.max(1, cores - Math.max(2, Math.ceil(cores * .2))) },
59  };
60}
61export function recommendedConfig(engine: EngineConfig, hardware: Hardware, context = 'recommended', use: ComputerUse = 'shared'): ConfigDocument {
62  return resourceConfig({
63    schema: 'strata.engine/1', batchAutomatic: true,
64    beginner: { priority: 'balanced', contextLength: context as ConfigDocument['beginner']['contextLength'], readImages: false, gpuIdle: 'never' },
65    expert: { ...Object.fromEntries(engine.expert.map(p => [p.id, p.recommended])), 'ctx.length': context === 'recommended' ? '128K' : context.toUpperCase() },
66  }, use, hardware);
67}