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

14.2 KB
Raw Blame
import { withMediaFileSizes } from '../../lib/mediaFileInfo.js';
import { mapWithConcurrency } from '../../lib/mapWithConcurrency.js';
import { isTestRunner } from '../../lib/runtimeEnv.js';
import { listAssets, countAssets, galleryFacets, listMixedGalleryPage, collectionCovers, visibleImageCollections } from './db.js';
import { compactGalleryRecord } from './logic.js';

const escapeHatch = () => process.env.MEMORY_BACKEND === 'file' || isTestRunner();
const keyFor = ({ kind, data }) => `${kind}:${kind === 'image' ? data.filename : data.id}`;
const keyForRef = item => `${item.kind}:${item.ref}`;
const time = value => Number.isFinite(Date.parse(value)) ? Date.parse(value) : 0;
const text = value => typeof value === 'string' && value.trim() ? value : null;
const readImages = async diskReader => (diskReader || (await import('../imageGen/local.js')).listGallery)();

async function resolveScope({ collectionId, starred, mediaKeys, collectionSnapshot }) {
  let keys = mediaKeys;
  let excludeKeys;
  let orderedKeys;
  let collection;
  if (collectionId) {
    const store = collectionSnapshot ? null : await import('../mediaCollections.js');
    if (collectionId === 'unsorted') {
      excludeKeys = (collectionSnapshot || await store.listCollections()).flatMap(c => (c.items || []).map(keyForRef));
    } else {
      collection = collectionSnapshot ? collectionSnapshot.find(item => item.id === collectionId) : await store.getCollection(collectionId);
      orderedKeys = [...(collection?.items || [])]
        .sort((a, b) => time(b.addedAt) - time(a.addedAt)).map(keyForRef);
      keys = keys ? keys.filter(key => orderedKeys.includes(key)) : orderedKeys;
    }
  }
  if (starred) {
    const annotations = await (await import('../mediaAnnotations.js')).listAnnotations();
    const ownStars = Object.entries(annotations).filter(([, entry]) => entry.own?.starred).map(([key]) => key);
    keys = keys ? keys.filter(key => ownStars.includes(key)) : ownStars;
  }
  return { mediaKeys: keys, excludeKeys, orderedKeys, collection };
}

/** Bounded reads; sidecars remain the reconcile authority and test escape hatch. */
export async function listGalleryPage({
  limit = 60, offset = 0, q = '', hidden, filename, starred = false, summary = false, compact = false,
  kind = 'image', media = false, cover = false, videoSnapshot, collectionSnapshot, collectionId, universeId, entryCategory, entryKind, mediaKeys,
} = {}, diskReader) {
  const mixed = media || kind !== 'image';
  const videoReader = mixed && videoSnapshot == null ? await import('../videoGen/history.js') : null;
  const videos = mixed ? videoSnapshot ?? await (compact && !q.trim()
    ? videoReader.loadCompactHistory() : videoReader.loadHistory()) : undefined;
  const scope = await resolveScope({ collectionId, starred, mediaKeys, collectionSnapshot });
  const filters = { kind: kind === 'all' ? undefined : kind, q, hidden, filename,
    universeId, entryCategory, entryKind, mediaKeys: scope.mediaKeys, excludeKeys: scope.excludeKeys, videos };
  let items;
  let total;
  let hiddenTotal;
  let counts;
  if (escapeHatch()) {
    const images = kind === 'video' && !summary ? [] : await readImages(diskReader);
    const tokens = q.trim().toLowerCase().split(/\s+/).filter(Boolean);
    const candidates = [...images.map(data => ({ kind: 'image', data })), ...(videos || []).map(data => ({ kind: 'video', data }))];
    const matching = (row, activeFilters, activeScope) =>
      (!activeFilters.kind || row.kind === activeFilters.kind)
      && (filename === undefined || ((row.kind === 'image' ? row.data.filename : row.data.id) === filename || row.data.filename === filename))
      && (activeScope.mediaKeys === undefined || activeScope.mediaKeys.includes(keyFor(row)))
      && !activeScope.excludeKeys?.includes(keyFor(row))
      && Object.entries({ universeId, entryCategory, entryKind }).every(([key, value]) => value === undefined || row.data[key] === value)
      && tokens.every(token => [JSON.stringify(row.data), row.kind,
        row.data.width && row.data.height ? `${row.data.width}x${row.data.height}` : '',
        row.data.extractedFromVideoId ? 'extracted frame' : '', row.data.stitchedFrom ? 'stitched' : '',
        row.data.upscaledFrom ? 'upscaled 2x' : ''].join(' ').toLowerCase().includes(token));
    const matches = candidates.filter(row => matching(row, filters, scope));
    const visible = matches.filter(row => hidden === undefined || !!row.data.hidden === hidden);
    visible.sort((a, b) => scope.orderedKeys
      ? scope.orderedKeys.indexOf(keyFor(a)) - scope.orderedKeys.indexOf(keyFor(b))
      : time(b.data.createdAt) - time(a.data.createdAt) || keyFor(a).localeCompare(keyFor(b)));
    items = visible.filter(row => !cover || row.kind === 'image' || row.data.thumbnail).slice(offset, offset + limit).map(row => mixed ? row : row.data);
    total = visible.length;
    hiddenTotal = matches.filter(row => !!row.data.hidden).length;
    if (summary && mixed) {
      // Kind-chip counts follow search and collection, before kind/favorites.
      const countScope = starred ? await resolveScope({ collectionId, mediaKeys, collectionSnapshot }) : scope;
      const counted = candidates.filter(row => matching(row, { ...filters, kind: undefined }, countScope)
        && (hidden === undefined || !!row.data.hidden === hidden));
      counts = { image: counted.filter(row => row.kind === 'image').length,
        video: counted.filter(row => row.kind === 'video').length, all: counted.length };
    }
    if (compact) {
      items = items.map(item => mixed ? { kind: item.kind, data: compactGalleryRecord(item.data) } : compactGalleryRecord(item));
    }
  } else if (summary && mixed) {
    // The DB reader returns compact rows itself: without a search it never
    // sends or buffers full prompts/detail bodies (#9676).
    const countScope = starred ? await resolveScope({ collectionId, mediaKeys, collectionSnapshot }) : scope;
    ({ items, total, hiddenTotal, counts } = await listMixedGalleryPage({
      ...filters, limit, offset, orderedKeys: scope.orderedKeys, cover, countMediaKeys: countScope.mediaKeys, compact,
    }));
  } else {
    [items, total, hiddenTotal] = await Promise.all([
      listAssets({ ...filters, limit, offset, typed: mixed, orderedKeys: scope.orderedKeys, cover, compact }),
      countAssets({ ...filters, compact }), summary ? countAssets({ ...filters, hidden: true, compact }) : undefined,
    ]);
  }
  items = await withMediaFileSizes(items, mixed ? undefined : kind);
  return { items, total, limit, offset, ...(summary ? { hiddenTotal, ...(counts ? { counts } : {}) } : {}) };
}

/** Global options contain descriptors and ids, never image metadata payloads. */
export async function listGalleryFacets(diskReader) {
  const rows = escapeHatch() ? (await readImages(diskReader)).filter(item => !item.hidden) : await galleryFacets();
  const universes = new Map();
  const categories = new Set();
  const kinds = new Set();
  for (const row of rows) {
    if (text(row.universeId)) universes.set(row.universeId, text(row.universeName) || row.universeId);
    if (text(row.entryCategory)) categories.add(row.entryCategory);
    if (text(row.entryKind)) kinds.add(row.entryKind);
  }
  const collections = await (await import('../mediaCollections.js')).listCollections();
  const populated = !escapeHatch() ? await visibleImageCollections(collections) : await mapWithConcurrency(collections, 4, async collection => {
    const page = await listGalleryPage({ collectionId: collection.id, collectionSnapshot: collections, hidden: false, limit: 1 }, diskReader);
    return page.total ? { id: collection.id, name: collection.name } : null;
  });
  return { universes: [...universes].map(([id, name]) => ({ id, name })),
    categories: [...categories], kinds: [...kinds], collections: populated.filter(Boolean) };
}

const FLAT_PNG_COVER = /^\/data\/images\/([A-Za-z0-9][A-Za-z0-9._-]*)\.png$/;
const VIDEO_THUMB_COVER = /^\/data\/video-thumbnails\/([A-Za-z0-9][A-Za-z0-9._-]*)\.[A-Za-z0-9]+$/;

/**
 * Card-sized cover URL: a flat gallery PNG maps to its lazily built 512px WebP
 * (`/data/image-thumbnails`) instead of the multi-MB original, and a cover whose
 * asset is neither local nor peer-hosted becomes null so the card never issues
 * a request that is certain to fail. Other shapes pass through unchanged.
 */
async function projectCovers(summaries, videoSnapshot) {
  // A thumbnail's stem is not always the video id (posters, stitched renders),
  // so probe by the real video filename recorded beside the thumbnail.
  const videoByThumb = new Map(videoSnapshot.filter(v => v.thumbnail && v.filename).map(v => [v.thumbnail, v.filename]));
  const { resolveMediaLocations } = await import('../peerHostedMedia.js');
  const probes = summaries.map(({ cover }) => {
    const png = FLAT_PNG_COVER.exec(cover || '');
    if (png) return { kind: 'image', ref: `${png[1]}.png`, url: `/data/image-thumbnails/${png[1]}.webp` };
    const thumb = VIDEO_THUMB_COVER.exec(cover || '');
    const videoFilename = thumb && videoByThumb.get(cover.slice('/data/video-thumbnails/'.length));
    if (videoFilename) return { kind: 'video', ref: videoFilename, url: cover };
    return null;
  });
  const located = await resolveMediaLocations(probes.filter(Boolean));
  let next = 0;
  return summaries.map((summary, i) => {
    const probe = probes[i];
    if (!probe) return summary;
    return { ...summary, cover: located[next++].location === 'missing' ? null : probe.url };
  });
}

/** Collection grid covers and counts, without downloading all image sidecars. */
export async function listGalleryCollectionSummaries(diskReader) {
  const videoSnapshot = await (await import('../videoGen/history.js')).loadCompactHistory();
  return projectCovers(await collectionSummaryRows(diskReader, videoSnapshot), videoSnapshot);
}

async function collectionSummaryRows(diskReader, videoSnapshot) {
  const collections = await (await import('../mediaCollections.js')).listCollections();
  if (!escapeHatch()) {
    const rows = await collectionCovers(collections.map(collection => ({
      id: collection.id, coverKey: collection.coverKey,
      keys: [...(collection.items || [])].sort((a, b) => time(b.addedAt) - time(a.addedAt)).map(keyForRef),
    })), videoSnapshot);
    const covers = new Map(rows.map(row => [row.id, row]));
    return [{ id: 'unsorted' }, ...collections].map(collection => {
      const row = covers.get(collection.id);
      const counts = collection.id === 'unsorted'
        ? { image: Number(row.image), video: Number(row.video), all: Number(row.image) + Number(row.video) }
        : (collection.items || []).reduce((result, item) => { result[item.kind]++; return result; }, { image: 0, video: 0 });
      return { id: collection.id, counts, total: counts.image + counts.video, cover: row?.cover ?? null };
    });
  }
  const summaries = await mapWithConcurrency([{ id: 'unsorted' }, ...collections], 4, async collection => {
    const page = await listGalleryPage({ collectionId: collection.id, media: true, kind: 'all', limit: 1, summary: collection.id === 'unsorted', cover: true, videoSnapshot, collectionSnapshot: collections }, diskReader);
    let cover = page.items[0];
    if (collection.coverKey) {
      const kind = collection.coverKey.startsWith('image:') ? 'image' : 'video';
      const filename = collection.coverKey.slice(kind.length + 1);
      const pinned = await listGalleryPage({ collectionId: collection.id, kind, media: true, filename, limit: 1, cover: true, videoSnapshot, collectionSnapshot: collections }, diskReader);
      if (pinned.items[0]) cover = pinned.items[0];
    }
    const counts = collection.id === 'unsorted' ? page.counts : (collection.items || []).reduce((result, item) => {
      result[item.kind]++; return result;
    }, { image: 0, video: 0 });
    return { id: collection.id, counts, total: counts.image + counts.video,
      cover: cover?.kind === 'image' ? cover.data.path || `/data/images/${cover.data.filename}`
        : cover?.data.thumbnail ? `/data/video-thumbnails/${cover.data.thumbnail}` : null };
  });
  return summaries;
}

/**
 * Resolve one image's original-vs-cleaned variant set: the group root plus
 * every record whose `cleanedFrom` names it. One call answers the lightbox
 * toggle from either end — pass a cleaned copy and it walks back to the root
 * first — so no caller has to hold a list wide enough to contain both.
 *
 * An image with no copies yields just its own root; `[]` means the filename is
 * not indexed at all.
 *
 * @param {string} filename - gallery basename of the image the user opened
 * @param {Function} [diskReader] - listGallery override for the test/file escape hatch
 * @returns {Promise<object[]>} gallery-shaped records: root first, then its copies
 */
export async function listImageVariants(filename, diskReader) {
  if (typeof filename !== 'string' || !filename) return [];
  // A variant's `cleanedFrom` is anchored at the ROOT original by every writer
  // in imageGen/variants.js, so one hop always lands on the group root. An
  // auto-cleaned image replaced its source in place and carries no
  // `cleanedFrom`, so it IS the root — matching the client's grouping.
  const rootOf = image => text(image.cleanedFrom) || image.filename;
  const isRoot = image => image && !text(image.cleanedFrom);

  if (escapeHatch()) {
    // One snapshot for all three lookups: each read is a directory scan plus a
    // sidecar read per image, so re-reading per lookup would cost 3N file reads
    // to answer one toggle.
    const images = await readImages(diskReader);
    const self = images.find(image => image?.filename === filename);
    if (!self) return [];
    const root = rootOf(self);
    const original = images.find(image => image?.filename === root && isRoot(image));
    return withMediaFileSizes([...(original ? [original] : []), ...images.filter(image => image?.cleanedFrom === root)], 'image');
  }

  const [self] = await listAssets({ kind: 'image', filename, limit: 1 });
  if (!self) return [];
  const root = rootOf(self);
  // Independent once the root is known — serially they would cost an extra
  // round trip on every cleaned-copy open.
  const [[original], copies] = await Promise.all([
    root === filename ? [self] : listAssets({ kind: 'image', filename: root, limit: 1 }),
    listAssets({ kind: 'image', cleanedFrom: root }),
  ]);
  return withMediaFileSizes([...(isRoot(original) ? [original] : []), ...copies], 'image');
}