Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

33.8 KB
Raw Blame
#!/usr/bin/env node
/**
 * The GPU pre-flight's requirement index (workflows/comfyui/requirements.json, src/lib/gpu/types.ts RequirementIndex):
 * per workflow its core node classes, its packs' classes, the model files it needs, VRAM, worker and first-run downloads;
 * per pack its pinned commit and registry id; per file its folder, URL pinned to a revision, size, sha256 and how a
 * ComfyUI shows it is there (the loader node's combo list, the folder listing, or not at all).
 *
 * Built from the committed library alone, no ComfyUI and no network: manifest.json, the workflow graphs,
 * object_info.subset.json (which classes are core and which belong to a pack), packs.json (pins, kept by hand),
 * model-pins.json (revisions and sizes), model-variants.json (other files of the same models, kept by hand) and
 * stream-needs.json (the smaller card and the RAM a workflow runs on with its weights streamed from RAM, and the packs
 * and workflows that never stream, kept by hand). It runs at the end of build-library.mjs and alone:
 *
 *   node scripts/comfy/build-requirements.mjs                 # write requirements.json
 *   node scripts/comfy/build-requirements.mjs --refresh-pins  # maintainers: re-read model-pins.json from Hugging Face
 *                                                             # (network; gated repos need HF_TOKEN), then write
 */
import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { PACK_CLASSES } from './library.spec.mjs';

const HERE = path.dirname(fileURLToPath(import.meta.url));
export const REPO = path.resolve(HERE, '..', '..');
export const LIBRARY = path.join(REPO, 'workflows', 'comfyui');

/** ComfyUI's folder_paths.supported_pt_extensions: the files a folder listing shows. */
export const PT_EXTENSIONS = ['.ckpt', '.pt', '.pt2', '.bin', '.pth', '.safetensors', '.pkl', '.sft'];
/**
 * Model folders a pack reads straight from disk without registering them with ComfyUI, so GET /models never lists them
 * and a folder listing can't tell whether their files are there: their files are 'unobservable' rather than missing.
 * Hi3D: Comfyui-Hi3d-T8 reads folder_paths.models_dir/Hi3D (a 'hi3d' folder only exists when extra_model_paths.yaml adds one).
 * sams: ComfyUI-Qwen-Watermark reads models/sams by its path without registering it, so a listing can't tell whether its
 * SAM file is there.
 */
export const UNLISTED_FOLDERS = new Set(['Hi3D', 'sams']);

/** Core classes of the newest families, read one by one when /object_info can't be: an older ComfyUI lacks them first. */
const NEWEST_CORE = /^(SeedVR2|Trellis2|SAM3|LTXV|LTXAV|ComfyMathExpression|TextEncodeQwenImage21|QwenImage21Cache|Ideogram4|TextEncodeFibo2|DualModelGuider|CFGOverride|MiniMaxH3ImageConditioning|TextGenerateV2)/;
const NEWEST_CORE_MODULES = new Set(['comfy_extras.nodes_bg_removal']);

// A pack that registers other packs' nodes again under its own name: on a ComfyUI that has it, /object_info gives
// its module for their classes (1,700 of them on one server), so it never names a class's pack.
const WRAPPER_PACKS = new Set(['comfyui-workflow-encrypt']);

/**
 * The pack a custom class belongs to, from its python_module on the server: the pack PACK_CLASSES names for it, else
 * the module's pack, else (a wrapper pack's module) the pack the committed object_info.subset.json recorded. Throws
 * when none of these tells.
 */
export function packOfClass(cls, pythonModule, recordedModule, packClasses = PACK_CLASSES) {
  const named = Object.entries(packClasses).find(([, re]) => re.test(cls))?.[0];
  if (named) return named;
  const pack = /^custom_nodes\.([^.]+)/.exec(pythonModule || '')?.[1];
  if (pack && !WRAPPER_PACKS.has(pack)) return pack;
  const recorded = /^custom_nodes\.([^.]+)/.exec(recordedModule || '')?.[1];
  if (recorded && !WRAPPER_PACKS.has(recorded)) return recorded;
  throw new Error(`can't tell which pack ${cls} belongs to (the server names ${pack ?? 'none'}); add it to PACK_CLASSES`);
}

/** A manifest URL as it is pinned and compared: without the `?download=true` some templates carry. */
export const cleanUrl = (u) => u.replace(/\?download=true$/, '');

/** A Hugging Face resolve URL → { kind: "models" | "datasets", repo, rev, path }, or null. */
export function hfParts(url) {
  const m = /^https:\/\/huggingface\.co\/(datasets\/)?([^/]+\/[^/]+)\/resolve\/([^/]+)\/(.+)$/.exec(cleanUrl(url));
  return m ? { kind: m[1] ? 'datasets' : 'models', repo: m[2], rev: m[3], path: decodeURIComponent(m[4]) } : null;
}

/** The URL at a revision (resolve/main/… → resolve/<sha>/…). */
export function pinUrl(url, revision) {
  const p = hfParts(url);
  if (!p || !revision) return cleanUrl(url);
  return cleanUrl(url).replace(`/resolve/${p.rev}/`, `/resolve/${revision}/`);
}

const isCombo = (spec) => Array.isArray(spec) && (Array.isArray(spec[0]) || spec[0] === 'COMBO');
const comboInput = (entry, input) => !!entry && isCombo(entry.input?.required?.[input] ?? entry.input?.optional?.[input]);
const sortObj = (o) => Object.fromEntries(Object.keys(o).sort().map((k) => [k, o[k]]));
const uniq = (a) => [...new Set(a)].sort();

/** JSON with sorted keys, no whitespace: what the hash is taken of. */
export function canonical(v) {
  if (Array.isArray(v)) return `[${v.map(canonical).join(',')}]`;
  if (v && typeof v === 'object') return `{${Object.keys(v).sort().filter((k) => v[k] !== undefined).map((k) => `${JSON.stringify(k)}:${canonical(v[k])}`).join(',')}}`;
  return JSON.stringify(v);
}

export const indexHash = (index) => crypto.createHash('sha256').update(canonical({ ...index, hash: undefined })).digest('hex');

/**
 * CPU work: a manifest workflow that loads no model file, fetches none on its first run and says it needs no graphics
 * memory (`vramGb: 0`, written as a number): joins, trims, adding sound, Ken Burns, still to clip. The index marks it
 * `cpu: true`. A missing or misspelt vramGb never makes a workflow CPU work (buildIndex refuses it), and neither does a
 * card its nodes need (`gpu`: the NVIDIA RTX Video nodes run on the card, with no model file and no VRAM written).
 */
export const cpuWork = (wf) => typeof wf.vramGb === 'number' && wf.vramGb === 0 && !wf.gpu && !(wf.models ?? []).length && !(wf.autoDownloads ?? []).length;

/**
 * Build the index from the library's inputs. Pure: no file or network access.
 * @param {{ manifest: object, graphs: Record<string, object>, subset: Record<string, object | null>, packs: Record<string, object>, pins: Record<string, { revision: string, sizeBytes: number, sha256?: string }>, variants?: { variants: object[] }, streams?: { streams: object[] }, packClasses?: Record<string, RegExp> }} inputs
 * @returns {import('../../src/lib/gpu/types').RequirementIndex}
 */
export function buildIndex({ manifest, graphs, subset, packs, pins, variants = { variants: [] }, streams = { streams: [] }, packClasses = PACK_CLASSES }) {
  const problems = [];
  /** A class's owner: "core", or a pack name. */
  const ownerOf = (cls, wf) => {
    const e = subset[cls];
    if (e && typeof e.python_module === 'string') {
      const m = /^custom_nodes\.([^.]+)/.exec(e.python_module);
      return m ? m[1] : 'core';
    }
    // Classes the validator couldn't load (null): their pack is named by PACK_CLASSES, else by the workflow's only pack.
    for (const [pack, re] of Object.entries(packClasses)) if (re.test(cls)) return pack;
    const named = wf.customNodes.map((c) => c.name);
    if (named.length === 1) return named[0];
    problems.push(`${wf.id}: can't tell which pack ${cls} belongs to`);
    return null;
  };

  // Files: one entry per folder/file; a file re-hosted at several URLs takes the URL most workflows use.
  const refs = new Map(); // key -> { model, urls: Map<url, count>, gated, wfs: Set }
  for (const wf of manifest.workflows) {
    for (const m of wf.models) {
      const key = `${m.folder}/${m.file}`;
      const r = refs.get(key) ?? { folder: m.folder, file: m.file, sizeGb: m.sizeGb ?? 0, urls: new Map(), gated: false, wfs: new Set() };
      if (m.url) r.urls.set(cleanUrl(m.url), (r.urls.get(cleanUrl(m.url)) ?? 0) + 1);
      r.gated ||= !!m.gated;
      r.sizeGb = Math.max(r.sizeGb, m.sizeGb ?? 0);
      r.wfs.add(wf.id);
      refs.set(key, r);
    }
  }
  const packFolder = new Map();
  for (const [name, p] of Object.entries(packs)) for (const f of p.folders ?? []) packFolder.set(f, name);
  // A pack that registers models/<folder> under another name (kandinsky6: models/audio_vae as kandinsky6_audio_vae):
  // ComfyUI lists the folder only under that name, so the presence check reads that listing.
  const listAs = new Map();
  for (const p of Object.values(packs)) for (const [f, as] of Object.entries(p.listAs ?? {})) listAs.set(f, as);

  const files = {};
  for (const key of [...refs.keys()].sort()) {
    const r = refs.get(key);
    const url = [...r.urls.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
    if (!url) {
      problems.push(`${key}: no download URL`);
      continue;
    }
    const pin = pins[url];
    // The loader nodes that pick this file in the workflows that need it (the input must be a combo: its list is what ComfyUI can load).
    const loaders = new Map();
    for (const id of r.wfs) {
      for (const node of Object.values(graphs[id] ?? {})) {
        for (const [input, value] of Object.entries(node?.inputs ?? {})) {
          if (value === r.file && comboInput(subset[node.class_type], input)) loaders.set(`${node.class_type}\u0000${input}`, [node.class_type, input]);
        }
      }
    }
    const ext = path.extname(r.file).toLowerCase();
    // An optional file no graph names (a GGUF the app binds in place of the default one): its loader is the bound input that holds
    // a file of the same folder and extension, when the file can't be seen in a model list.
    if (!loaders.size && !(PT_EXTENSIONS.includes(ext) && !UNLISTED_FOLDERS.has(r.folder))) {
      for (const id of r.wfs) {
        const wf = manifest.workflows.find((w) => w.id === id);
        for (const targets of Object.values(wf?.bindings ?? {})) {
          for (const { node, input } of targets) {
            const n = graphs[id]?.[node];
            const held = n?.inputs?.[input];
            const same = typeof held === 'string' && path.extname(held).toLowerCase() === ext && wf.models.some((m) => m.file === held && m.folder === r.folder);
            if (same && comboInput(subset[n.class_type], input)) loaders.set(`${n.class_type}\u0000${input}`, [n.class_type, input]);
          }
        }
      }
    }
    const presence = loaders.size ? 'loader' : PT_EXTENSIONS.includes(ext) && !UNLISTED_FOLDERS.has(r.folder) ? 'list' : 'unobservable';
    files[key] = {
      folder: r.folder,
      file: r.file,
      url,
      pinnedUrl: pin ? pinUrl(url, pin.revision) : url,
      sizeBytes: pin?.sizeBytes ?? Math.round(r.sizeGb * 1e9),
      // The hash rides along where a copied command checks it: formats that can run code when they load (.pth, .ckpt…),
      // the other non-safetensors files, and a file from outside Hugging Face (a release asset, which its owner can replace).
      // Every file's sha256 stays in model-pins.json (the size budget, §4.3).
      ...(pin?.sha256 && (ext !== '.safetensors' || !hfParts(url)) && { sha256: pin.sha256 }),
      gated: r.gated,
      ...(packFolder.has(r.folder) && { packFolder: packFolder.get(r.folder) }),
      ...(listAs.has(r.folder) && { listFolder: listAs.get(r.folder) }),
      presence,
      ...(loaders.size && { loaders: [...loaders.values()].sort((a, b) => (a[0] + a[1]).localeCompare(b[0] + b[1])) }),
    };
  }

  // Workflows: classes split into core and packs.
  const packClassesUsed = new Map();
  const workflows = {};
  for (const wf of manifest.workflows) {
    const graph = graphs[wf.id];
    if (!graph) {
      problems.push(`${wf.id}: no workflow graph`);
      continue;
    }
    if (typeof wf.vramGb !== 'number' || !Number.isFinite(wf.vramGb) || wf.vramGb < 0) problems.push(`${wf.id}: vramGb must be a number of GB (0 when no node uses graphics memory)`);
    const core = new Set();
    const byPack = {};
    for (const n of wf.customNodes) byPack[n.name] ??= new Set();
    for (const cls of new Set([...Object.values(graph).map((n) => n.class_type), ...(wf.runtimeNodes ?? [])])) {
      const owner = ownerOf(cls, wf);
      if (!owner) continue;
      if (owner === 'core') core.add(cls);
      else {
        (byPack[owner] ??= new Set()).add(cls);
        if (!packClassesUsed.has(owner)) packClassesUsed.set(owner, new Set());
        packClassesUsed.get(owner).add(cls);
      }
    }
    workflows[wf.id] = {
      core: uniq(core),
      packs: sortObj(Object.fromEntries(Object.entries(byPack).map(([k, v]) => [k, uniq(v)]))),
      files: uniq(wf.models.filter((m) => !m.optional).map((m) => `${m.folder}/${m.file}`)),
      optional: uniq(wf.models.filter((m) => m.optional).map((m) => `${m.folder}/${m.file}`)),
      vramGb: Number(wf.vramGb) || 0,
      isolated: wf.worker === 'isolated',
      // The card its nodes need (the NVIDIA RTX Video nodes): readiness checks it against the GPU's name.
      ...(wf.gpu && { gpu: wf.gpu }),
      firstRun: wf.autoDownloads ?? [],
      ...(wf.validated && { validated: true }),
      ...(cpuWork(wf) && { cpu: true }),
    };
  }

  // Variants: other files of the same models a workflow may load in place of some of its own (model-variants.json).
  const built = modelVariants({ variants, workflows, files, graphs, subset, pins });
  problems.push(...built.problems);
  for (const [id, list] of Object.entries(built.byWorkflow)) workflows[id] = { ...workflows[id], variants: list };

  // Streaming needs: the smaller card and the RAM a workflow runs on with part of its weights in RAM (stream-needs.json),
  // or false for a workflow that never streams.
  const streamed = streamNeeds({ streams, workflows, packs });
  problems.push(...streamed.problems);
  for (const [id, stream] of Object.entries(streamed.byWorkflow)) workflows[id] = { ...workflows[id], stream };

  // Packs: every pack a workflow uses, with its pins from packs.json.
  const usedPacks = uniq(Object.values(workflows).flatMap((w) => Object.keys(w.packs)));
  const indexPacks = {};
  for (const name of usedPacks) {
    const p = packs[name];
    if (!p) {
      problems.push(`${name}: not in packs.json`);
      continue;
    }
    indexPacks[name] = {
      repo: p.repo,
      commit: p.commit,
      // A node file Fuobe ships itself (fuobe_live.py): written into custom_nodes, never cloned.
      ...(p.bundled && { bundled: p.bundled }),
      ...(p.registry && { registry: { id: p.registry.id, version: p.registry.version ?? '' } }),
      classes: uniq(packClassesUsed.get(name) ?? []),
      folders: uniq(p.folders ?? []),
      isolated: !!p.isolated,
      ...(p.subdir && { subdir: p.subdir }),
      // One node file in a model repo (viggle_turbo): downloaded, not cloned.
      ...(p.file && { file: p.file }),
      requirementsTxt: !!p.requirementsTxt,
      // Python packages to install by name instead of the pack's requirements.txt (pinned versions only).
      ...(Array.isArray(p.pip) && p.pip.length && { pip: [...p.pip] }),
      install: p.registry?.version ? 'registry' : 'manual',
    };
  }

  const coreNewest = Object.entries(subset)
    .filter(([cls, e]) => e && !String(e.python_module).startsWith('custom_nodes.') && (NEWEST_CORE.test(cls) || NEWEST_CORE_MODULES.has(e.python_module)))
    .map(([cls]) => cls);
  const used = new Set(Object.values(workflows).flatMap((w) => w.core));
  const sentinels = uniq([...Object.values(indexPacks).flatMap((p) => p.classes), ...coreNewest.filter((c) => used.has(c))]);

  if (problems.length) throw new Error(`requirements.json can't be built:\n  ${problems.join('\n  ')}`);
  const index = {
    v: 1,
    hash: '',
    comfyui: { min: manifest.comfyuiVersion, testedCommit: String(manifest.comfyuiCommit ?? '').slice(0, 8) },
    // The files' folders, and the folders a used pack picks its models from by name without registering them
    // (packs.json `modelFolders`: H3-FaceRefine's detectors under models/ultralytics), so a worker's
    // extra_model_paths.yaml registers them and ComfyUI lists their files.
    folders: uniq([...Object.values(files).map((f) => f.listFolder ?? f.folder), ...usedPacks.flatMap((name) => packs[name]?.modelFolders ?? [])]),
    sentinels,
    packs: indexPacks,
    files,
    workflows: sortObj(workflows),
  };
  index.hash = indexHash(index);
  return index;
}

/** ComfyUI reads an older folder name as the same folder: `clip` for `text_encoders`, `unet` for `diffusion_models`. */
const FOLDER_KIND = { clip: 'text_encoders', unet: 'diffusion_models' };
const folderKind = (folder) => FOLDER_KIND[folder] ?? folder;

/** A card generation as nvidia-smi gives it (its CUDA compute capability): "8.6", "12.0". */
const GPU_GEN = /^\d{1,2}\.\d{1,2}$/;
const genValue = (g) => {
  const [major, minor] = g.split('.').map(Number);
  return major * 100 + minor;
};

/** A variant's name: lower case letters, digits and dashes. */
const VARIANT_ID = /^[a-z0-9][a-z0-9-]{0,39}$/;

/**
 * Each workflow's variants from model-variants.json, checked against the built files and workflows: `byWorkflow` (workflow
 * id → its variants, in the file's order, which is the order of preference) and `problems` (the build stops on any).
 * A variant swaps some of a workflow's own files for other files of the same model. It is refused when its new file is
 * missing from the catalog, has no pinned size and SHA-256, sits in another model folder, or isn't read by each loader
 * input that reads the old file in the workflow's graph (that loader input reads the new file in no catalog workflow).
 *
 * @param {{ variants: { variants?: object[] }, workflows: Record<string, { files: string[] }>, files: Record<string, object>, graphs: Record<string, object>, subset: Record<string, object | null>, pins: Record<string, { sizeBytes?: number, sha256?: string }> }} o
 */
export function modelVariants({ variants, workflows, files, graphs, subset, pins }) {
  const problems = [];
  const byWorkflow = {};
  const list = variants?.variants;
  if (!Array.isArray(list)) return { byWorkflow, problems: ['model-variants.json: "variants" must be a list'] };
  const sha256Of = (f) => {
    const pin = pins[f.url] ?? pins[f.pinnedUrl];
    const sha = f.sha256 ?? pin?.sha256;
    return typeof sha === 'string' && /^[0-9a-f]{64}$/.test(sha) && pin?.sizeBytes === f.sizeBytes ? sha : null;
  };
  list.forEach((v, i) => {
    const at = `model-variants.json variant ${i + 1}${typeof v?.id === 'string' ? ` (${v.id})` : ''}`;
    if (!v || typeof v !== 'object') {
      problems.push(`${at}: not an object`);
      return;
    }
    if (typeof v.id !== 'string' || !VARIANT_ID.test(v.id)) problems.push(`${at}: id must be lower case letters, digits and dashes`);
    let gpuGen;
    if (v.gpuGen !== undefined) {
      const { min, max } = v.gpuGen ?? {};
      if (typeof min !== 'string' || typeof max !== 'string' || !GPU_GEN.test(min) || !GPU_GEN.test(max) || genValue(min) > genValue(max)) {
        problems.push(`${at}: gpuGen must be { "min": "8.0", "max": "8.6" } (compute capabilities, the lower first)`);
      } else gpuGen = { min, max };
    }
    if (v.vramGb !== undefined && (typeof v.vramGb !== 'number' || !Number.isFinite(v.vramGb) || v.vramGb <= 0)) problems.push(`${at}: vramGb must be a number of GB`);
    const swap = v.files && typeof v.files === 'object' && !Array.isArray(v.files) ? Object.entries(v.files) : [];
    if (!swap.length) problems.push(`${at}: files must map at least one of the workflow's files to another`);
    for (const [from, to] of swap) {
      const a = files[from];
      const b = typeof to === 'string' ? files[to] : undefined;
      if (!a) problems.push(`${at}: ${from} isn't a file of the catalog`);
      if (!b) problems.push(`${at}: ${to} isn't a file of the catalog (a workflow of the manifest must name it)`);
      if (!a || !b) continue;
      if (from === to) problems.push(`${at}: ${from} is swapped for itself`);
      if (folderKind(a.folder) !== folderKind(b.folder)) problems.push(`${at}: ${to} is in another model folder than ${from}`);
      if (!sha256Of(b)) problems.push(`${at}: ${to} has no pinned size and SHA-256 (model-pins.json)`);
    }
    const ids = Array.isArray(v.workflows) ? v.workflows : [];
    if (!ids.length) problems.push(`${at}: workflows must name at least one workflow`);
    for (const id of ids) {
      const wf = workflows[id];
      if (!wf) {
        problems.push(`${at}: ${id} isn't a workflow of the manifest`);
        continue;
      }
      if ((byWorkflow[id] ?? []).some((x) => x.id === v.id)) problems.push(`${at}: ${id} has two variants named ${v.id}`);
      for (const [from, to] of swap) {
        const a = files[from];
        const b = files[to];
        if (!a || !b) continue;
        if (!wf.files.includes(from)) {
          problems.push(`${at}: ${from} isn't one of ${id}'s files`);
          continue;
        }
        // The loader inputs that read the old file in this graph: each must read the new one too.
        const slots = new Set();
        for (const node of Object.values(graphs[id] ?? {})) {
          for (const [input, value] of Object.entries(node?.inputs ?? {})) {
            if (value === a.file && comboInput(subset[node.class_type], input)) slots.add(`${node.class_type}.${input}`);
          }
        }
        if (!slots.size) problems.push(`${at}: ${id}'s graph loads ${from} through no loader input, so nothing can be swapped`);
        // A file no graph loads, named only as one of this workflow's optional files (a smaller build of the same model):
        // the loader input it fills is the slot it is swapped into.
        if (!b.loaders?.length && wf.optional?.includes(to)) continue;
        const takes = new Set((b.loaders ?? []).map(([cls, input]) => `${cls}.${input}`));
        for (const s of slots) if (!takes.has(s)) problems.push(`${at}: ${id} loads ${from} with ${s}, which reads ${to} in no workflow of the catalog`);
      }
      (byWorkflow[id] ??= []).push({ id: v.id, files: sortObj(Object.fromEntries(swap)), ...(gpuGen && { gpuGen }), ...(v.vramGb !== undefined && { vramGb: v.vramGb }) });
    }
  });
  return { byWorkflow, problems };
}

/** The largest streaming need in GB, VRAM and RAM alike (a slip like 3200 for 32 is refused). */
const STREAM_MAX_GB = 2048;

/**
 * Each workflow's streaming need from stream-needs.json: the graphics memory and the system RAM a GPU needs to run it with
 * part of its model weights kept in RAM and streamed to the card at each step (ComfyUI's dynamic VRAM loading). It runs
 * slower than with every weight on the card. `byWorkflow` (workflow id → { vramGb, ramGb }) and `problems` (the build
 * stops on any). An entry is refused when a number isn't above 0, when its VRAM isn't below the workflow's own VRAM
 * need, when it names a workflow the manifest doesn't have, and when a workflow is named twice.
 *
 * A workflow with no streaming need of its own may still stream by the computed rule (src/lib/lanes.ts streamFits: the
 * size of the files it loads against the GPU's VRAM and RAM), unless it never streams: `noStream` names the node packs
 * (`packs`) and the workflows (`workflows`) whose models are loaded outside ComfyUI's model management (a pack's own
 * loader and offloading), which dynamic VRAM loading can't stream. An isolated workflow never streams either: it runs on
 * a ComfyUI of its own. Each of these gets `false` in `byWorkflow`. A name the catalog doesn't have is refused, and so is
 * a workflow that has a streaming need and never streams.
 *
 * @param {{ streams: { streams?: object[], noStream?: { packs?: string[], workflows?: string[] } }, workflows: Record<string, { vramGb: number, packs?: Record<string, string[]>, isolated?: boolean }>, packs?: Record<string, object> }} o
 */
export function streamNeeds({ streams, workflows, packs = {} }) {
  const problems = [];
  const byWorkflow = {};
  const list = streams?.streams;
  if (!Array.isArray(list)) return { byWorkflow, problems: ['stream-needs.json: "streams" must be a list'] };
  for (const k of Object.keys(streams ?? {})) if (k !== 'streams' && k !== 'noStream') problems.push(`stream-needs.json: unknown field ${k}`);
  const gb = (v) => typeof v === 'number' && Number.isFinite(v) && v > 0 && v <= STREAM_MAX_GB;
  const known = new Set(['workflows', 'vramGb', 'ramGb']);
  list.forEach((e, i) => {
    const at = `stream-needs.json entry ${i + 1}`;
    if (!e || typeof e !== 'object' || Array.isArray(e)) {
      problems.push(`${at}: not an object`);
      return;
    }
    for (const k of Object.keys(e)) if (!known.has(k)) problems.push(`${at}: unknown field ${k}`);
    if (!gb(e.vramGb)) problems.push(`${at}: vramGb must be a number of GB above 0`);
    if (!gb(e.ramGb)) problems.push(`${at}: ramGb must be a number of GB above 0`);
    const ids = Array.isArray(e.workflows) ? e.workflows : [];
    if (!ids.length) problems.push(`${at}: workflows must name at least one workflow`);
    for (const id of ids) {
      const wf = typeof id === 'string' ? workflows[id] : undefined;
      if (!wf) {
        problems.push(`${at}: ${id} isn't a workflow of the manifest`);
        continue;
      }
      if (byWorkflow[id]) problems.push(`${at}: ${id} has a streaming need already`);
      if (gb(e.vramGb) && !(e.vramGb < wf.vramGb)) problems.push(`${at}: ${id} needs ${wf.vramGb} GB of VRAM, so its streaming need must be below that (not ${e.vramGb} GB)`);
      if (gb(e.vramGb) && gb(e.ramGb)) byWorkflow[id] = { vramGb: e.vramGb, ramGb: e.ramGb };
    }
  });
  // The workflows that never stream: noStream's packs and workflows, and every isolated workflow.
  const no = streams.noStream ?? {};
  const names = (k) => (Array.isArray(no[k]) && no[k].every((x) => typeof x === 'string') ? no[k] : null);
  const noPacks = names('packs');
  const noWorkflows = names('workflows');
  if (!no || typeof no !== 'object' || Array.isArray(no)) problems.push('stream-needs.json: "noStream" must be an object');
  else {
    for (const k of Object.keys(no)) if (k !== 'packs' && k !== 'workflows') problems.push(`stream-needs.json noStream: unknown field ${k}`);
    if (no.packs !== undefined && !noPacks) problems.push('stream-needs.json noStream: packs must be a list of node pack names');
    if (no.workflows !== undefined && !noWorkflows) problems.push('stream-needs.json noStream: workflows must be a list of workflow ids');
  }
  for (const p of noPacks ?? []) if (!packs[p]) problems.push(`stream-needs.json noStream: ${p} isn't a node pack of packs.json`);
  for (const id of noWorkflows ?? []) if (!workflows[id]) problems.push(`stream-needs.json noStream: ${id} isn't a workflow of the manifest`);
  const packSet = new Set(noPacks ?? []);
  const wfSet = new Set(noWorkflows ?? []);
  for (const [id, wf] of Object.entries(workflows)) {
    const why = wf.isolated ? 'it runs isolated' : wfSet.has(id) ? 'noStream names it' : Object.keys(wf.packs ?? {}).find((p) => packSet.has(p));
    if (!why) continue;
    if (byWorkflow[id]) {
      problems.push(`stream-needs.json: ${id} has a streaming need, but it never streams (${packSet.has(why) ? `it uses ${why}` : why})`);
      continue;
    }
    byWorkflow[id] = false;
  }
  return { byWorkflow, problems };
}

/** The inputs as committed. */
export function loadInputs(dir = LIBRARY) {
  const read = (f) => JSON.parse(fs.readFileSync(path.join(dir, f), 'utf8'));
  const manifest = read('manifest.json');
  const graphs = Object.fromEntries(manifest.workflows.map((w) => [w.id, read(w.file)]));
  const pinsFile = path.join(dir, 'model-pins.json');
  const variantsFile = path.join(dir, 'model-variants.json');
  const streamsFile = path.join(dir, 'stream-needs.json');
  return {
    manifest,
    graphs,
    subset: read('object_info.subset.json'),
    packs: read('packs.json'),
    pins: fs.existsSync(pinsFile) ? read('model-pins.json') : {},
    variants: fs.existsSync(variantsFile) ? read('model-variants.json') : { variants: [] },
    streams: fs.existsSync(streamsFile) ? read('stream-needs.json') : { streams: [] },
    packClasses: PACK_CLASSES,
  };
}

/**
 * The index as written: one line per pack, file and workflow (reviewable diffs, small file).
 * JSON.parse of it equals the index.
 */
export function formatIndex(index) {
  const block = (o) => `{\n${Object.entries(o).map(([k, v]) => `    ${JSON.stringify(k)}: ${JSON.stringify(v)}`).join(',\n')}\n  }`;
  const top = Object.entries(index).map(([k, v]) => `  ${JSON.stringify(k)}: ${['packs', 'files', 'workflows'].includes(k) ? block(v) : JSON.stringify(v)}`);
  return `{\n${top.join(',\n')}\n}\n`;
}

export function writeIndex(dir = LIBRARY) {
  const index = buildIndex(loadInputs(dir));
  const text = formatIndex(index);
  fs.writeFileSync(path.join(dir, 'requirements.json'), text);
  return { index, bytes: Buffer.byteLength(text) };
}

// ---------------------------------------------------------------------------
// --refresh-pins: revisions, sizes and sha256 from Hugging Face (maintainers; network)

/** The largest file kept in git itself (not LFS) that a refresh reads to hash. */
export const SMALL_FILE_BYTES = 1024 * 1024;

/** A small file's sha256, read at a revision; it must be exactly `size` bytes. */
async function smallFileSha(fetchImpl, g, sha, filePath, size, headers) {
  const prefix = g.kind === 'datasets' ? 'datasets/' : '';
  const r = await fetchImpl(`https://huggingface.co/${prefix}${g.repo}/resolve/${sha}/${filePath.split('/').map(encodeURIComponent).join('/')}`, { headers });
  if (!r.ok) throw new Error(`HTTP ${r.status}`);
  const bytes = Buffer.from(await r.arrayBuffer());
  if (bytes.length !== size) throw new Error('size differs');
  return crypto.createHash('sha256').update(bytes).digest('hex');
}

/**
 * Pin every manifest URL: the repo's current revision (GET /api/<kind>/<repo>/revision/main) and, at that revision,
 * each file's size and LFS sha256 (POST /api/<kind>/<repo>/paths-info/<sha>), or for a small file kept in git itself
 * (up to SMALL_FILE_BYTES) the sha256 of its bytes at that revision. URLs already at a commit keep it. Files that can't
 * be read (gated without HF_TOKEN, moved) keep their old pin, or none; they are returned in `failed`.
 */
export async function refreshPins(dir = LIBRARY, { token = process.env.HF_TOKEN, fetchImpl = fetch, log = console.log } = {}) {
  const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'manifest.json'), 'utf8'));
  const pinsFile = path.join(dir, 'model-pins.json');
  const old = fs.existsSync(pinsFile) ? JSON.parse(fs.readFileSync(pinsFile, 'utf8')) : {};
  const urls = uniq(manifest.workflows.flatMap((w) => w.models.flatMap((m) => (m.url ? [cleanUrl(m.url)] : []))));
  const headers = { 'User-Agent': 'Fuobe build-requirements', ...(token && { Authorization: `Bearer ${token}` }) };
  // Group by repo and revision asked for.
  const groups = new Map();
  const failed = [];
  const byHand = [];
  for (const url of urls) {
    const p = hfParts(url);
    if (!p) {
      // A release asset elsewhere (Zen Image Edit's adapter on GitHub): its pin is kept by hand.
      failed.push({ url, why: 'not a Hugging Face resolve URL (pin kept by hand)' });
      byHand.push(url);
      continue;
    }
    const k = `${p.kind}\u0000${p.repo}\u0000${p.rev}`;
    if (!groups.has(k)) groups.set(k, { ...p, items: [] });
    groups.get(k).items.push({ url, path: p.path });
  }
  const pins = Object.fromEntries(byHand.flatMap((url) => (old[url] ? [[url, old[url]]] : [])));
  for (const g of groups.values()) {
    let sha = /^[0-9a-f]{40}$/.test(g.rev) ? g.rev : null;
    try {
      if (!sha) {
        const r = await fetchImpl(`https://huggingface.co/api/${g.kind}/${g.repo}/revision/${encodeURIComponent(g.rev)}`, { headers });
        if (!r.ok) throw new Error(`revision: HTTP ${r.status}`);
        sha = (await r.json()).sha;
        if (!/^[0-9a-f]{40}$/.test(sha ?? '')) throw new Error('revision: no sha');
      }
      const r = await fetchImpl(`https://huggingface.co/api/${g.kind}/${g.repo}/paths-info/${sha}`, {
        method: 'POST',
        headers: { ...headers, 'Content-Type': 'application/json' },
        body: JSON.stringify({ paths: g.items.map((i) => i.path), expand: false }),
      });
      if (!r.ok) throw new Error(`paths-info: HTTP ${r.status}`);
      const info = new Map((await r.json()).map((e) => [e.path, e]));
      for (const it of g.items) {
        const e = info.get(it.path);
        if (!e || e.type !== 'file') {
          failed.push({ url: it.url, why: 'not found at that revision' });
          if (old[it.url]) pins[it.url] = old[it.url];
          continue;
        }
        const size = e.lfs?.size ?? e.size;
        // A large file's sha256 is its LFS oid. A small file kept in git itself (a config.json) has none: it is read
        // at that revision and hashed.
        let sha256 = e.lfs?.oid && /^[0-9a-f]{64}$/.test(e.lfs.oid) ? e.lfs.oid : null;
        if (!e.lfs && Number.isInteger(size) && size <= SMALL_FILE_BYTES) sha256 = await smallFileSha(fetchImpl, g, sha, it.path, size, headers).catch(() => null);
        pins[it.url] = { revision: sha, sizeBytes: size, ...(sha256 && { sha256 }) };
      }
      log(`✓ ${g.repo}@${sha.slice(0, 8)}: ${g.items.length} file(s)`);
    } catch (e) {
      log(`! ${g.repo}: ${e.message}`);
      for (const it of g.items) {
        failed.push({ url: it.url, why: e.message });
        if (old[it.url]) pins[it.url] = old[it.url];
      }
    }
  }
  fs.writeFileSync(pinsFile, `${JSON.stringify(sortObj(pins), null, 1)}\n`);
  return { pinned: Object.keys(pins).length, failed };
}

async function main() {
  if (process.argv.includes('--refresh-pins')) {
    const r = await refreshPins();
    console.log(`model-pins.json: ${r.pinned} pinned, ${r.failed.length} not pinned`);
    for (const f of r.failed) console.log(`  ${f.url}: ${f.why}`);
  }
  const { index, bytes } = writeIndex();
  console.log(`requirements.json: ${Object.keys(index.workflows).length} workflows, ${Object.keys(index.files).length} files, ${Object.keys(index.packs).length} packs, ${bytes} bytes`);
}

if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
  main().catch((e) => {
    console.error(e.message ?? e);
    process.exit(1);
  });
}