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.6 KB
Raw Blame
/**
 * Files completed Music Studio audio jobs onto their track, even when the
 * browser that started the render has unmounted (#4353).
 */
import { renderFederatedMediaAudioPrompt } from '../lib/federatedMediaWire.js';
import { createMediaJobImageHook } from './mediaJobImageHook.js';
import { updateJobResult } from './mediaJobQueue/index.js';
import * as tracks from './tracks/index.js';
import { createTrackWithAlbum } from './trackAlbumMembership.js';

const hook = createMediaJobImageHook({
  label: 'Music Studio',
  initLog: '🎵 Music Studio completion hook initialized',
  kind: 'audio',
  tagKey: 'musicStudio',
  identify: (tag) => (tag && (tag.trackId === null || typeof tag.trackId === 'string')
    ? { trackId: tag.trackId || null } : null),
  serializeKey: ({ trackId }) => trackId || 'standalone',
  describe: ({ trackId }) => trackId || 'standalone',
  extractResult: (job) => {
    const filename = job.result?.filename;
    if (typeof filename !== 'string' || !filename) return null;
    const tag = job.params.musicStudio;
    const authoredPrompt = typeof tag.authoredPrompt === 'string' ? tag.authoredPrompt : job.params.prompt;
    const authoredLyrics = typeof tag.authoredLyrics === 'string' ? tag.authoredLyrics : job.params.lyrics;
    const remotePrompt = renderFederatedMediaAudioPrompt(job.params.remoteMedia?.profile);
    return {
      filename,
      durationSec: Number.isFinite(job.result?.durationSec) ? Math.round(job.result.durationSec) : null,
      engine: typeof job.result?.engine === 'string' ? job.result.engine : null,
      modelId: typeof job.result?.modelId === 'string' ? job.result.modelId : null,
      executionProfile: typeof job.result?.executionProfile === 'string' ? job.result.executionProfile : null,
      prompt: remotePrompt || job.params.prompt,
      authoredPrompt,
      ...(typeof tag.sentPrompt === 'string' ? { sentPrompt: tag.sentPrompt, promptAdditions: tag.promptAdditions } : {}),
      lyrics: remotePrompt ? '' : job.params.lyrics,
      authoredLyrics,
      ...(typeof tag.sentLyrics === 'string' ? { sentLyrics: tag.sentLyrics, lyricAdditions: tag.lyricAdditions } : {}),
      lyricsEnabled: tag.lyricsEnabled === true,
      lyricsProvided: tag.lyricsProvided === true,
      instrumentalOnly: typeof tag.instrumentalOnly === 'boolean' ? tag.instrumentalOnly : null,
      title: tag.title,
      artistId: tag.artistId,
      artist: tag.artist,
      albumId: tag.albumId,
    };
  },
  attach: async ({ job, trackId, filename, durationSec, engine, modelId, executionProfile, prompt, authoredPrompt, sentPrompt, promptAdditions, sentLyrics, lyricAdditions, lyrics, authoredLyrics, lyricsEnabled, lyricsProvided, instrumentalOnly, title, artistId, artist, albumId }) => {
    const current = trackId ? await tracks.getTrack(trackId) : null;
    // A deleted target is a successful render but no longer an attach target.
    if (trackId && !current) return null;
    const { renders } = tracks.buildRenderAppend(current, {
      audioFilename: filename,
      prompt,
      authoredPrompt,
      sentPrompt,
      promptAdditions,
      sentLyrics,
      lyricAdditions,
      lyrics: lyricsEnabled ? lyrics : '',
      instrumentalOnly,
      engine,
      modelId,
      executionProfile,
      durationSec,
    });
    const meta = {
      audioFilename: filename,
      engine,
      modelId,
      durationSec,
      prompt: authoredPrompt,
      renders,
    };
    // Existing instrumental renders preserve the track's saved lyric draft;
    // a standalone render has no prior record, so retain its authored lyrics.
    const shouldPersistLyrics = lyricsEnabled && lyricsProvided && (instrumentalOnly !== true || !trackId);
    if (shouldPersistLyrics) meta.lyrics = authoredLyrics;
    const { track, albumAssignmentError } = trackId
      ? { track: await tracks.updateTrack(trackId, meta) }
      : await createTrackWithAlbum({
        title: title || authoredPrompt.slice(0, 60),
        artistId,
        artist,
        albumId,
        ...(shouldPersistLyrics ? { lyrics: authoredLyrics } : {}),
        ...meta,
      }, { preserveUnassigned: true });
    await updateJobResult(job.id, { trackId: track.id, ...(albumAssignmentError ? { albumAssignmentError } : {}) });
    if (albumAssignmentError) console.warn(`⚠️ Music Studio saved completed audio as a single: ${albumAssignmentError.message}`);
    return track;
  },
  onAttached: ({ trackId, filename }, track) => {
    console.log(`🎵 Music Studio ${trackId || track?.id || 'new'} ← ${filename}`);
  },
});

export function initMusicStudioHook() {
  hook.init();
}

export const __testing = hook.__testing;