okami.mom/strata

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

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

main
1.2 KiB27 linesraw
1/** Locate catalog weights in a Strata folder or directly inside an existing GGUF folder. */
2import { closeSync, existsSync, openSync, readSync, realpathSync, statSync } from 'node:fs';
3import { isAbsolute, join, relative } from 'node:path';
4
5export function matchesGGUF(file, bytes, directory) {
6  let fd;
7  try {
8    if (!existsSync(file)) return false;
9    const path = relative(realpathSync(directory), realpathSync(file));
10    if (path.startsWith('..') || isAbsolute(path)) return false;
11    const stat = statSync(file);
12    if (!stat.isFile() || stat.size !== bytes) return false;
13    fd = openSync(file, 'r');
14    const magic = Buffer.alloc(4); readSync(fd, magic, 0, 4, 0);
15    return magic.toString() === 'GGUF';
16  } catch { return false; }
17  finally { if (fd !== undefined) closeSync(fd); }
18}
19
20export function locateModelFile(directory, model) {
21  const nested = join(directory, model.id, model.source.file);
22  if (matchesGGUF(nested, model.source.bytes, directory)) return nested;
23  const flat = join(directory, model.source.file);
24  // The design catalog deliberately has ambiguous filenames across model families.
25  if (!model.designOnly && matchesGGUF(flat, model.source.bytes, directory)) return flat;
26  return null;
27}