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

6.7 KB
Raw Blame
import { request } from './apiCore.js';

// Static-asset URL for a track's audio bytes in the shared music library
// (data/music/). Not an API call — a plain href for <audio>/download links.
export const trackAudioUrl = (filename) => `/data/music/${encodeURIComponent(filename)}`;

// ---- Music tracks ----
// A track is a single song/recording — standalone or part of an album. It stores
// a pointer (`audioFilename`) into the shared music library; the bytes are
// uploaded/generated there. `options` lets a caller suppress request()'s
// auto-toast with `{ silent: true }`.
export const listTracks = (options = {}) => request('/tracks', options);
export const getTrack = (id, options = {}) => request(`/tracks/${encodeURIComponent(id)}`, options);
export const createTrack = (data, requestOptions = {}) => request('/tracks', {
  method: 'POST',
  body: JSON.stringify(data),
  ...requestOptions,
});
export const updateTrack = (id, patch, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}`, {
  method: 'PATCH',
  body: JSON.stringify(patch),
  ...requestOptions,
});
export const deleteTrack = (id, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}`, {
  method: 'DELETE',
  ...requestOptions,
});

// Shared music library (uploaded + generated tracks under data/music/). Used to
// attach an existing track without re-uploading.
export const listMusicLibrary = (options = {}) => request('/tracks/library', options);

// --- YouTube import (#1945) — download + extract a track's audio via yt-dlp,
// land it in the shared library, and create a Track. Kickoff returns { jobId };
// progress streams over SSE (subscribe with useSseProgress).
export const importTrackFromYoutube = (url, options = {}) => request('/tracks/import/youtube', {
  method: 'POST',
  body: JSON.stringify({ url }),
  ...options,
});
// A Suno song link: the song's audio plus the title, lyrics and style its page carries.
export const importTrackFromSuno = (url, options = {}) => request('/tracks/import/suno', {
  method: 'POST',
  body: JSON.stringify({ url }),
  ...options,
});

export const trackImportEventsUrl = (jobId) =>
  `/api/tracks/import/${encodeURIComponent(jobId)}/events`;

export const cancelTrackImport = (jobId, options = {}) => request(`/tracks/import/${encodeURIComponent(jobId)}/cancel`, {
  method: 'POST',
  ...options,
});

// Attach an existing library track (by stored filename) to this track.
export const attachTrackAudio = (id, filename, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}/audio/attach`, {
  method: 'POST',
  body: JSON.stringify({ filename }),
  ...requestOptions,
});

// --- Render history ----
// A track keeps every generated/uploaded take in `track.renders[]`. The active
// take is mirrored onto the top-level `audioFilename`/`engine`/`modelId`/
// `durationSec`. Both endpoints return `{ track }` (the updated record).

// Make a past render the active one (re-points the player + gen-metadata badges).
export const selectTrackRender = (id, renderId, requestOptions = {}) => request(
  `/tracks/${encodeURIComponent(id)}/renders/${encodeURIComponent(renderId)}/select`,
  { method: 'POST', ...requestOptions },
);

// Remove a render from the history (the audio bytes stay in the shared library).
export const deleteTrackRender = (id, renderId, requestOptions = {}) => request(
  `/tracks/${encodeURIComponent(id)}/renders/${encodeURIComponent(renderId)}`,
  { method: 'DELETE', ...requestOptions },
);

// Upload an audio file (FormData with a `track` file field) into the library and
// attach it to this track. The caller builds the FormData; we pass it through as
// the request body so the multipart boundary header is set by the browser.
export const uploadTrackAudio = (id, formData, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}/audio/upload`, {
  method: 'POST',
  body: formData,
  ...requestOptions,
});

// --- Chiptune scores (#2911) — prompt-based looping 8-bit background music.

// Generate (or iterate on) the track's chiptune score with an AI provider.
// `body`: { prompt, providerId?, model?, fresh? }.
export const generateTrackChiptune = (id, body, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}/chiptune/generate`, {
  method: 'POST',
  body: JSON.stringify(body),
  ...requestOptions,
});

// Render the current score into the shared music library as a looping take.
export const renderTrackChiptune = (id, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}/chiptune/render`, {
  method: 'POST',
  ...requestOptions,
});

// Publish the current score into a managed app's repo (OGG + score JSON).
// `body`: { appId, subdir?, slug? }.
export const publishTrackChiptune = (id, body, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}/chiptune/publish`, {
  method: 'POST',
  body: JSON.stringify(body),
  ...requestOptions,
});

// Have the LLM DRAW the music as a wave sketch (server/lib/waveSketch.js) and
// store it on the track (`waveSketch`/`waveSketchPrompt`, #8376).
// `body`: { description, lyrics?, guidance?, durationSec?, revise? (redraw the
// stored sketch), providerId?, model?, effort? } → { sketch, llm, track }.
export const drawTrackWaveform = (id, body, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}/waveform/draw`, {
  method: 'POST',
  body: JSON.stringify(body),
  ...requestOptions,
});

// Render the track's STORED wave sketch into the shared music library as the
// track's active take. `body`: { prompt?, title? } → { track, filename, durationSec }.
export const renderTrackWaveform = (id, body, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}/waveform/render`, {
  method: 'POST',
  body: JSON.stringify(body),
  ...requestOptions,
});

// Save a take recorded from the code engine's sandboxed player. `formData`
// carries the WAV as a `track` file plus optional `prompt`/`title` fields.
// → { track, filename, durationSec }.
export const renderTrackCode = (id, formData, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}/code/render`, {
  method: 'POST',
  body: formData,
  ...requestOptions,
});

// Save a finished SuperCollider preview as the active take. The server reads
// the audio and provenance (source, hash, seed, runtime) from its own preview
// store, so only the job id and optional prompt/title travel. → { track, filename, durationSec }.
export const saveSuperColliderTake = (id, body, requestOptions = {}) => request(`/tracks/${encodeURIComponent(id)}/supercollider/take`, {
  method: 'POST',
  body: JSON.stringify(body),
  ...requestOptions,
});

// Caps come from the server leaf so client inputs and the server sanitizer can't drift.
export { TITLE_MAX as TRACK_TITLE_MAX, LYRICS_MAX as TRACK_LYRICS_MAX, PROMPT_MAX as TRACK_PROMPT_MAX } from '../../../server/lib/trackLimits.js';