Files
strata/apps/host/server/models.mjs
T
Niko1221 0b3299a019 apps/host: the owner's UI and the Node App Host
The UI source from the 14 October handoff (src/, public/, index.html, vite and
tsconfig, tests, audit-fixes, design docs) and the App Host (server/*.mjs): model
import and monitor routes, API-provider catalog and streaming (OpenAI chat, OpenAI Responses,
the Messages API), serving, tunnel, tasks, plus the host routes for Time-skip
(server/timeskip.mjs) and the tuning plan/reports. src/lib/time-skip.ts renders a real
job's own record.

The UI design is unchanged. The rebuild routed #/tuning to a new TunePanel; that is
reverted: src/screens/ModelRun.tsx and src/main.tsx are the handoff's again (#/tuning
goes where the handoff sends it), and src/components/TunePanel.tsx and
src/styles/tuning.css, which only that route used, are not carried over. Left out of
git on purpose: dist/ (build output), node_modules, and docs/evidence + docs/checkpoints
(170 MB of screenshots, kept in StrataSplit\evidence-archive).

From the owner's rebuild branch strata/rebuild-2026-10.
2026-10-15 10:24:09 +02:00

446 lines
16 KiB
JavaScript

/**
* Model library.
*
* Downloads are real: weights stream from Hugging Face into the configured model folder (Documents/Strata/Models by default)
* with progress, resume-by-restart and a size check. Loading starts a real
* engine process through the supervisor — no model files, no process, no load.
*/
import { createWriteStream, existsSync, mkdirSync, renameSync, rmSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { CONTEXT_TOKENS, VARIANTS, downloadUrl, footprint, localModels, refreshLocalCatalog, variant } from './catalog.mjs';
/**
* Design-iteration mode.
*
* Strata is being iterated on as a product surface, so by default the model
* library is simulated: downloads animate, loads stage, nothing is fetched and
* no process is spawned. Set STRATA_REAL_ENGINE=1 to use the real path
* (Hugging Face download + supervised llama.cpp engine).
*/
export const REAL = process.env.STRATA_REAL_ENGINE === '1';
import { MODELS_DIR, diskFreeGb, get as getStore, update } from './store.mjs';
import * as supervisor from './supervisor.mjs';
import { EventHub, nowIso, round, uid } from './util.mjs';
import { locateModelFile } from './model-files.mjs';
/** In-memory runtime state that never needs persisting. */
const runtime = new Map(); // id -> { state, progress, error }
const downloads = new Map(); // id -> AbortController
let hubRef = null;
export function pathFor(v) {
if (v.local) return v.local.path;
return locateModelFile(MODELS_DIR, v) ?? join(MODELS_DIR, v.id, v.source.file);
}
export function isOnDisk(id) {
const v = variant(id);
if (!v) return false;
if (v.local) return localModels.ready(v);
// In design mode "installed" is a state file entry, not a file on disk.
if (!REAL && getStore().installed?.[id]?.simulated) return true;
return !!locateModelFile(MODELS_DIR, v);
}
export function diskUsage(id) {
const v = variant(id);
if (!v) return 0;
if (v.local) return v.local.files.reduce((n, f) => n + f.bytes, 0) / 1024 ** 3;
if (!existsSync(pathFor(v))) return REAL ? 0 : v.diskGb; // simulated installs keep the catalog size
return statSync(pathFor(v)).size / 1024 ** 3;
}
export function init(hub) {
hubRef = hub;
supervisor.init(hub);
if (!REAL) {
const previous = getStore();
const old = Object.keys(previous.installed ?? {}).filter(id => !id.startsWith('local-') && !VARIANTS.some(v=>v.id===id));
if (old.length) {
const balanced=VARIANTS.find(v=>v.quant==='Q4_K_M').id;
update({installed:{...previous.installed,[balanced]:{installedAt:nowIso(),simulated:true}},
...(old.includes(previous.loaded)?{loaded:balanced}:{}),...(old.includes(previous.current)?{current:balanced}: {})});
}
}
runtime.clear();
for (const v of VARIANTS) {
runtime.set(v.id, { state: isOnDisk(v.id) ? 'installed' : 'available' });
}
// Trust the filesystem over the JSON state file.
const s = getStore();
const installed = { ...(s.installed ?? {}) };
for (const id of Object.keys(installed)) if (!VARIANTS.some(v => v.id === id)) delete installed[id];
for (const v of VARIANTS) {
if (isOnDisk(v.id) && !installed[v.id]) installed[v.id] = { installedAt: nowIso() };
if (!isOnDisk(v.id) && installed[v.id]) delete installed[v.id];
}
const patch = { installed };
if (s.loaded && !isOnDisk(s.loaded)) patch.loaded = null;
if (s.current && !isOnDisk(s.current)) patch.current = null;
// Files on disk are the source of truth for "current", so a state file that
// disagrees with the filesystem heals itself.
const onDisk = VARIANTS.filter((v) => isOnDisk(v.id));
if (!patch.current && !s.current && onDisk.length) {
patch.current = (onDisk.find((v) => v.tier === 'starter') ?? onDisk[0]).id;
}
update(patch);
}
export function library() {
const s = getStore();
const usedGb = VARIANTS.reduce((sum, v) => sum + (isOnDisk(v.id) ? diskUsage(v.id) : 0), 0);
const engine = supervisor.status();
const activeDownloads = [...runtime.values()].filter((r) => r.state === 'downloading').length;
return {
mode: REAL ? 'real' : 'simulated',
current: s.current,
loaded: REAL ? (engine.running ? engine.modelId : null) : s.loaded,
variants: VARIANTS.map((v) => {
const entry = runtime.get(v.id) ?? { state: 'available' };
const onDisk = isOnDisk(v.id);
const loading = REAL ? engine.status === 'starting' && engine.modelId === v.id : entry.state === 'loading';
const isLoaded = REAL ? engine.running && engine.modelId === v.id : s.loaded === v.id;
const state = loading ? 'loading' : entry.state === 'error' ? 'error' : onDisk ? (isLoaded ? 'loaded' : 'installed') : v.local ? 'error' : entry.state;
return {
...v,
catalog: {
family: v.family.startsWith('Qwen3-Coder') ? 'Qwen Coder' : 'Qwen',
version: v.family,
purpose: v.family.includes('Coder') ? 'Coding-focused instruction model' : 'General text and conversation',
capabilities: v.family.includes('Coder') ? ['Text', 'Code'] : ['Text'],
agentApproved: false,
balancedRank: v.role === 'best-fit' ? 1 : v.tier === 'starter' ? 10 : 5,
review: 'Capability review pending',
},
diskGb: onDisk ? round(diskUsage(v.id), 2) : v.diskGb,
state,
progress: entry.progress,
error: v.local && !onDisk ? 'The local file is missing or has changed. Use a local model again to reconnect it.' : entry.error,
installedAt: v.local ? onDisk ? v.importedAt : undefined : s.installed?.[v.id]?.installedAt,
downloadUrl: downloadUrl(v),
};
}),
downloads: { active: activeDownloads, queued: 0, totalGbToday: round(usedGb, 2) },
disk: { modelsDir: MODELS_DIR, usedGb: round(usedGb, 2), freeGb: round(diskFreeGb(MODELS_DIR), 0) },
engine: { status: engine.status, binary: engine.binary, port: engine.port, modelId: engine.modelId },
};
}
function emit() {
hubRef?.send('models', library());
}
/* ---------------------------------------------------------------- download --- */
export async function install(id) {
const v = variant(id);
if (!v) throw new Error(`Unknown model ${id}`);
if (v.local) throw new Error('Local models are not downloaded. Choose their GGUF file again to reconnect them.');
if (isOnDisk(id)) {
const s = getStore();
update({ installed: { ...(s.installed ?? {}), [id]: s.installed?.[id] ?? { installedAt: nowIso() } }, current: s.current ?? id });
emit();
return library();
}
if (downloads.has(id)) return library();
if (!REAL) return simulateInstall(v);
const dir = join(MODELS_DIR, v.id);
mkdirSync(dir, { recursive: true });
const finalPath = pathFor(v);
const partPath = `${finalPath}.part`;
const controller = new AbortController();
downloads.set(id, controller);
const startedAt = Date.now();
runtime.set(id, {
state: 'downloading',
progress: { pct: 0, mbps: 0, etaSec: 0, stage: `Fetching ${v.source.file}` },
});
emit();
hubRef?.send('log', { level: 'info', message: `Downloading ${v.label} from ${v.source.repo}`, at: nowIso() });
// Keep the UI fed even while the socket is quiet.
const ticker = setInterval(() => {
const entry = runtime.get(id);
if (!entry || entry.state !== 'downloading') return;
emit();
}, 400);
try {
const res = await fetch(downloadUrl(v), { signal: controller.signal, redirect: 'follow' });
if (!res.ok || !res.body) throw new Error(`Download failed (${res.status} ${res.statusText})`);
const total = Number(res.headers.get('content-length')) || v.source.bytes;
let received = 0;
let lastEmit = 0;
const progressStream = new ReadableStream({
start(controllerStream) {
const reader = res.body.getReader();
const pump = async () => {
for (;;) {
const { done, value } = await reader.read();
if (done) break;
received += value.byteLength;
controllerStream.enqueue(value);
const now = Date.now();
if (now - lastEmit > 350) {
lastEmit = now;
const seconds = (now - startedAt) / 1000;
const mbps = received / 1024 ** 2 / Math.max(seconds, 0.001);
runtime.set(id, {
state: 'downloading',
progress: {
pct: (received / total) * 100,
mbps,
etaSec: mbps > 0 ? (total - received) / 1024 ** 2 / mbps : 0,
stage: received / total < 0.9 ? `Fetching ${v.source.file}` : 'Verifying and finalising',
},
});
}
}
controllerStream.close();
};
void pump().catch((err) => controllerStream.error(err));
},
});
await pipeline(Readable.fromWeb(progressStream), createWriteStream(partPath));
renameSync(partPath, finalPath);
const size = statSync(finalPath).size;
if (size < v.source.bytes * 0.9) {
throw new Error(`File is smaller than expected (${(size / 1e9).toFixed(2)} GB of ${(v.source.bytes / 1e9).toFixed(2)} GB)`);
}
const s = getStore();
update({
installed: { ...(s.installed ?? {}), [id]: { installedAt: nowIso(), bytes: size } },
current: s.current ?? id,
});
runtime.set(id, { state: 'installed' });
hubRef?.send('log', { level: 'info', message: `${v.label} installed (${(size / 1e9).toFixed(2)} GB)`, at: nowIso() });
} catch (err) {
const aborted = err?.name === 'AbortError';
try {
if (existsSync(partPath)) rmSync(partPath);
} catch {
/* ignore */
}
runtime.set(id, aborted ? { state: 'available' } : { state: 'error', error: err.message });
if (!aborted) hubRef?.send('log', { level: 'error', message: `Download failed: ${err.message}`, at: nowIso() });
} finally {
clearInterval(ticker);
downloads.delete(id);
emit();
}
return library();
}
export function cancel(id) {
const download = downloads.get(id);
download?.abort();
// Real jobs retain their guard until their finally block has settled.
if (download) return library();
runtime.set(id, { state: 'available' });
emit();
return library();
}
export function remove(id) {
const v = variant(id);
if (!v) return library();
const current = library();
if (current.loaded === id || current.variants.find(row => row.id === id)?.state === 'loading') throw new Error('Unload this model before deleting its files.');
if (v.local) {
localModels.remove(id); refreshLocalCatalog(); runtime.delete(id);
const s = getStore(), installed = { ...s.installed }; delete installed[id];
update({ installed, current: s.current === id ? null : s.current });
emit(); return library();
}
cancel(id);
try {
const file = pathFor(v);
// Existing folders may contain unrelated weights. Delete only this variant's file.
if (existsSync(file)) rmSync(file, { force: true });
} catch (err) {
hubRef?.send('log', { level: 'warn', message: `Could not delete ${v.family} ${v.quant}: ${err.message}`, at: nowIso() });
throw new Error(`Could not delete model files: ${err.message}`);
}
const s = getStore();
const installed = { ...(s.installed ?? {}) };
delete installed[id];
update({
installed,
loaded: s.loaded === id ? null : s.loaded,
current: s.current === id ? null : s.current,
});
runtime.set(id, { state: 'available' });
emit();
return library();
}
/* -------------------------------------------------------------------- load --- */
export async function load(id, hooks = {}) {
const v = variant(id);
if (!v) throw new Error(`Unknown model ${id}`);
if (!isOnDisk(id)) throw new Error(`${v.label} is not downloaded yet`);
const s = getStore();
const port = Number(s.expert?.['server.port'] ?? 8080);
const hardware = hooks.hardware;
const config = hooks.config;
if (!REAL) return simulateLoad(v, hooks);
runtime.set(id, { state: 'loading', progress: { pct: 10, mbps: 0, etaSec: 0, stage: 'Starting engine' } });
emit();
await hooks.onPhase?.('loading', id);
try {
await supervisor.restart({
modelId: id,
modelPath: pathFor(v),
port,
config: { ...config, layerCount: v.layerCount },
hardware,
});
} catch (err) {
runtime.set(id, { state: 'error', error: err.message });
update({ loaded: null });
emit();
throw err;
}
update({ loaded: id, current: id });
runtime.set(id, { state: 'loaded' });
emit();
await hooks.onPhase?.('ready', id);
return library();
}
export async function unload(hooks = {}) {
if (!REAL) {
update({ loaded: null });
const s = getStore();
if (s.current && runtime.has(s.current)) runtime.set(s.current, { state: 'installed' });
emit();
await hooks.onPhase?.('idle', null);
return library();
}
await supervisor.stop();
update({ loaded: null });
const s = getStore();
if (s.current && runtime.has(s.current)) runtime.set(s.current, { state: 'installed' });
emit();
await hooks.onPhase?.('idle', null);
return library();
}
/* ------------------------------------------------------- simulated (design) --- */
function simulateInstall(v) {
const totalSec = 10; // Preview timing is independent of fictional file sizes.
const startedAt = Date.now();
runtime.set(v.id, {
state: 'downloading',
progress: { pct: 0, mbps: 140, etaSec: totalSec, stage: `Fetching ${v.source.file}` },
});
emit();
const tick = setInterval(() => {
const elapsed = (Date.now() - startedAt) / 1000;
const pct = Math.min(100, (elapsed / totalSec) * 100);
const entry = runtime.get(v.id);
if (!entry || entry.state !== 'downloading') {
clearInterval(tick);
return;
}
runtime.set(v.id, {
state: 'downloading',
progress: {
pct,
mbps: 120 + Math.sin(elapsed * 2.2) * 28,
etaSec: Math.max(0, totalSec - elapsed),
stage: pct < 55 ? `Fetching ${v.source.file}` : pct < 88 ? 'Verifying shard hashes' : 'Finalising files',
},
});
emit();
if (pct >= 100) {
clearInterval(tick);
const s = getStore();
update({
installed: { ...(s.installed ?? {}), [v.id]: { installedAt: nowIso(), simulated: true } },
current: s.current ?? v.id,
});
runtime.set(v.id, { state: 'installed' });
hubRef?.send('log', { level: 'info', message: `${v.label} ready (simulated)`, at: nowIso() });
emit();
}
}, 280);
hubRef?.send('log', { level: 'info', message: `Downloading ${v.label} (simulated)`, at: nowIso() });
return library();
}
async function simulateLoad(v, hooks) {
runtime.set(v.id, { state: 'loading', progress: { pct: 12, mbps: 0, etaSec: 3, stage: 'Mapping weights into memory' } });
emit();
await hooks.onPhase?.('loading', v.id);
for (const pct of [38, 64, 86, 100]) {
await new Promise((r) => setTimeout(r, 420));
runtime.set(v.id, {
state: 'loading',
progress: { pct, mbps: 0, etaSec: Math.max(0, (100 - pct) / 30), stage: pct < 70 ? 'Mapping weights into memory' : 'Warming expert cache' },
});
emit();
}
update({ loaded: v.id, current: v.id });
runtime.set(v.id, { state: 'loaded' });
emit();
await hooks.onPhase?.('ready', v.id);
return library();
}
export function loadedVariant() {
const engine = supervisor.status();
const id = REAL ? (engine.running ? engine.modelId : null) : getStore().loaded;
return id ? variant(id) : null;
}
export function runtimeState(id) {
return runtime.get(id)?.state ?? 'available';
}
export function directoryBusy() {
return downloads.size > 0 || [...runtime.values()].some(r => ['loading', 'downloading'].includes(r.state)) || supervisor.status().status === 'starting';
}
export function newDownloadId() {
return uid('dl');
}
export { footprint, CONTEXT_TOKENS };
export function localModelAdded() {
refreshLocalCatalog();
emit();
return library();
}