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

2.2 KB
Raw Blame
import {
  importReferenceAudio,
  cancelReferenceAudioImport,
  referenceAudioImportEventsUrl,
} from '../services/apiRounds.js';
import useSseJobSlot from './useSseJobSlot.js';

/**
 * One round reference-audio-import job slot (#2120) — download + extract a
 * reference's audio from a URL via yt-dlp into the uploads dir, streaming SSE
 * progress. The deferred convenience path from the reference-audio analysis
 * feature (#2106); upload/mic capture remain the primary attach paths.
 *
 * A thin wrapper over the generic `useSseJobSlot` (#2368) — call it once per UI
 * surface that can independently kick off an import so each owns its own job +
 * SSE subscription (a shared slot would let a second kickoff orphan the first's
 * in-flight job).
 *
 * `onComplete(filename, context)` fires once the download lands a file in the
 * uploads dir — `context` is whatever was passed to `start(url, context)`,
 * captured at kickoff so a slow-finishing job still attaches to the right
 * target even if the caller's own state changed while it was in flight.
 *
 * `target` is `{ roundId, referenceId }` for a reference that is already saved
 * (or null for an unsaved draft row). It is read at each kickoff, and the server
 * attaches the finished file to that reference itself (#9943), so `onComplete`
 * only has to mirror it into the editor draft.
 */
export default function useReferenceAudioImport({ onComplete, target = null } = {}) {
  const { active, percent, stage, start, cancel } = useSseJobSlot({
    startRequest: (url) => importReferenceAudio(url, { ...target, silent: true }),
    eventsUrl: referenceAudioImportEventsUrl,
    cancelRequest: cancelReferenceAudioImport,
    trimStartArg: true,
    onComplete: (frame, context) => onComplete?.(frame.filename, context),
    successToast: (frame) => (frame.attached
      ? 'Reference audio downloaded and attached'
      : 'Reference audio downloaded — Save the song to keep it'),
    errorFallback: 'Reference audio download failed',
    canceledMessage: 'Reference audio download cancelled',
    lostConnectionMessage: 'Lost connection to the reference-audio download',
    startErrorFallback: 'Failed to start reference-audio download',
  });
  return { active, percent, stage, start, cancel };
}