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

4.1 KB
Raw Blame
/** User-facing video-history mutations. */

import { trashFiles } from '../../lib/trashFiles.js';
import { PATHS, atomicWrite } from '../../lib/fileUtils.js';
import { ServerError } from '../../lib/errorHandler.js';
import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';
import { safeUnder } from '../../lib/ffmpeg.js';
import { mutateVideoHistory } from './history.js';
import { extendMasterPath } from './extendMaster.js';

export async function updateHistoryItemPrompt(id, prompt) {
  let result;
  await mutateVideoHistory((history) => {
    const item = history.find((h) => h.id === id);
    if (!item) throw new ServerError('Not found', { status: 404, code: 'NOT_FOUND' });
    const trimmedPrompt = typeof prompt === 'string' ? prompt.trim() : '';
    if (trimmedPrompt) item.prompt = trimmedPrompt;
    else delete item.prompt;
    // The trigger-weave provenance (#4665) describes the prompt this render was
    // MADE with, so it can't survive an edit to that prompt — leaving it would
    // have the row claim a `renderPrompt` derived from text that is no longer
    // there, and name tokens as "added" to a prompt they were never added to.
    delete item.renderPrompt;
    delete item.addedTriggerWords;
    result = { id, prompt: trimmedPrompt };
    return history;
  });
  return result;
}

export async function setHistoryItemHidden(id, hidden) {
  let result;
  // Serialized find-and-set through the shared tail; a 404 throw inside the
  // mutator rejects before any save, preserving the not-found semantics.
  await mutateVideoHistory((history) => {
    const item = history.find((h) => h.id === id);
    if (!item) throw new ServerError('Not found', { status: 404, code: 'NOT_FOUND' });
    item.hidden = !!hidden;
    result = { ok: true, hidden: item.hidden };
    return history;
  });
  return result;
}

export async function deleteHistoryItem(id, { forgetInMusicVideos = true } = {}) {
  // One lease covers the history read, file removals, history rewrite, and
  // derived row removal; per-file admission would leave gaps between stores.
  return withBackupAssetPublication(async () => {
    let item;
    // Hold the history writer across its recovery snapshot and the file move,
    // so overlapping prompt edits/deletes cannot retire a different revision.
    await mutateVideoHistory(async history => {
      item = history.find((entry) => entry.id === id);
      if (!item) throw new ServerError('Not found', { status: 404, code: 'NOT_FOUND' });
      const videoFile = safeUnder(PATHS.videos, item.filename);
      if (!videoFile) throw new ServerError('Invalid video filename', { status: 400, code: 'INVALID_VIDEO_PATH' });
      // The shared history file stays in place; recycle a per-video copy of
      // this record alongside its bytes so prompts/settings remain recoverable.
      const metadataFile = `${videoFile}.metadata.json`;
      await atomicWrite(metadataFile, item);
      const files = [videoFile, metadataFile, extendMasterPath(videoFile)];
      if (item.thumbnail) files.push(safeUnder(PATHS.videoThumbnails, item.thumbnail));
      // Evaluation frames are derived; include all supported frame slots.
      for (let i = 1; i <= 9; i++) files.push(safeUnder(PATHS.videoThumbnails, `${id}-f${i}.jpg`));
      await trashFiles(files);
      return history.filter(entry => entry.id !== id);
    });
    // Drop the derived index row with the entry (#2738) — keyed by job id, the
    // ref the index wrote it under. Non-fatal + dynamically imported; see the
    // matching hook in imageGen/local.js#deleteImage for the rationale.
    await import('../mediaAssetIndex/index.js')
      .then((m) => m.unindexVideo(id))
      .catch((err) => console.error(`❌ Media index video delete hook: ${err.message}`));
    // Music videos forget it too (their unused takes, marks and collection rows);
    // a batch delete from a video's asset manager forgets its whole batch at once.
    if (forgetInMusicVideos) await import('../musicVideo/mediaPool.js').then((m) => m.forgetDeletedMusicVideoAssets([`video:${id}`]));
    console.log(`🗑️ Trashed video: ${item.filename}`);
    return { ok: true };
  });
}