Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

3.1 KB
Raw Blame
/**
 * Attach `speedProfiles` to shipped video models (issue #4875) in existing
 * installs.
 *
 * `data.reference/media-models.json` ships the profiles for fresh installs and
 * `mediaModels.js` backfills them at load — but `data/media-models.json` is the
 * gitignored, user-editable copy that is never merge-updated, so this migration
 * makes the change durable on disk instead of relying on the in-memory backfill
 * running every boot. Same arrangement as migration 237 (disclosures) and 238
 * (Finish edges).
 *
 * Preservation contract (identical to `applyVideoSpeedProfiles`):
 *   - an entry that already has a `speedProfiles` key is left alone (user edit,
 *     including an explicit `null` / `[]` meaning "no profiles on this model")
 *   - an id the user deleted is NOT recreated
 *   - a custom / user-added model is untouched (no shipped profile exists)
 *   - an entry whose `repo` OR `revision` was re-pointed is skipped — a
 *     validated sampler schedule must not be claimed for different weights
 *   - entry order is preserved; nothing else on the entry is modified
 *
 * Additive only: an entry the sanitizer merely STRIPPED is never rewritten, so
 * a hand-edited bad profile is left for the load-time sanitizer to warn about
 * rather than being silently deleted from the user's file here.
 */

import { readFile } from 'fs/promises';
import { join } from 'path';
import { atomicWrite } from '../../server/lib/fileUtils.js';
import { applyVideoSpeedProfiles } from '../../server/lib/videoSpeedProfiles.js';
import { LEGACY_VIDEO_BUCKET_KEYS, VIDEO_BUCKETS } from '../../server/lib/mediaModelBuckets.js';

const REL_PATH = 'data/media-models.json';
// Both the canonical (#4142) and the legacy bucket spellings — an install that
// predates the rename can still be on either shape. Only keys actually holding
// an array are touched, so listing all four is safe on both.
const BUCKET_KEYS = [...VIDEO_BUCKETS, ...Object.values(LEGACY_VIDEO_BUCKET_KEYS)];

export default {
  async up({ rootDir }) {
    const path = join(rootDir, REL_PATH);
    const raw = await readFile(path, 'utf-8').catch((err) => {
      if (err.code === 'ENOENT') return null;
      throw err;
    });
    if (raw == null) return;
    let config;
    try {
      config = JSON.parse(raw);
    } catch (err) {
      throw new Error(`Cannot migrate ${REL_PATH}: invalid JSON (${err.message})`, { cause: err });
    }
    if (!config?.video || typeof config.video !== 'object') return;

    let profiled = 0;
    for (const platform of BUCKET_KEYS) {
      const list = config.video[platform];
      if (!Array.isArray(list)) continue;
      const next = applyVideoSpeedProfiles(list);
      config.video[platform] = list.map((entry, i) => {
        const gained = Array.isArray(next[i]?.speedProfiles) && !(entry && 'speedProfiles' in entry);
        if (!gained) return entry;
        profiled += 1;
        return next[i];
      });
    }

    if (profiled > 0) {
      await atomicWrite(path, `${JSON.stringify(config, null, 2)}\n`);
      console.log(`📝 ${REL_PATH}: attached speed profiles to ${profiled} video ${profiled === 1 ? 'entry' : 'entries'}`);
    }
  },
};