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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
import { useRef, useState } from 'react';
import toast from '../components/ui/Toast';
import {
addMusicVideoSceneTake,
selectMusicVideoSceneTake,
placeMusicVideoPoolItem,
continueMusicVideoSceneFromPrevious,
reviewMusicVideoSceneTake,
rejectUnusedMusicVideoSceneTakes,
swapMusicVideoSceneFrames,
markMusicVideoPoolAssets,
getMusicVideoHandoff,
getMusicVideoHandoffBundle,
importMusicVideoHandoff,
} from '../services/apiMusicVideo.js';
import { uploadGalleryImage } from '../services/apiSystem.js';
import { trimVideoStart, uploadGalleryVideo } from '../services/apiImageVideo.js';
import { downloadBlob } from '../lib/downloadBlob.js';
import { validateImageFile } from '../utils/fileUpload.js';
// Mirrors the server's handoff import cap, checked BEFORE uploading so an
// oversized pick can't leave a gallery full of files nothing imported.
const MAX_HANDOFF_FILES = 200;
// The video formats the gallery video upload can container-sniff.
const HANDOFF_VIDEO_TYPES = new Set(['video/mp4', 'video/quicktime', 'video/webm']);
// Why a picked file can't be uploaded, or null.
function handoffFileProblem(file, isVideo) {
if (!isVideo) return validateImageFile(file, Infinity);
if (!HANDOFF_VIDEO_TYPES.has(file.type)) return `File "${file.name}" is not an MP4, MOV, or WebM video`;
return null;
}
const plural = (n, word) => `${n} ${word}${n === 1 ? '' : 's'}`;
const handoffSlug = (name) => String(name || 'music-video').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '') || 'music-video';
const handoffFilename = (name) => `${handoffSlug(name)}-handoff.json`;
const handoffBundleFilename = (name) => `${handoffSlug(name)}-handoff.zip`;
/**
* Scene-take review + external-asset handoff actions for the Music Video board
* (#8965). Every mutation resolves to the server's updated scene, which is
* folded back through `applyScenePatch` (selection fields + take list only), so
* a take action never clobbers a scene prompt the director is still editing.
* Take mutations run one at a time: a Reject followed by its note must land in
* order, or the older response could overwrite the newer take list locally.
*
* Handoff import uploads each picked file through the ordinary gallery upload
* routes (image → data/images, video → video history), then asks the server to
* associate them with scenes by the file tags the exported manifest assigned.
*/
export default function useMusicVideoTakes({ project, applyScenePatch, patchProject = null }) {
const [busy, setBusy] = useState(false);
const chainRef = useRef(Promise.resolve());
const applyScene = (projectId, scene) => applyScenePatch?.(projectId, scene.sceneId, {
referenceImageId: scene.referenceImageId ?? null,
videoHistoryId: scene.videoHistoryId ?? null,
endImageId: scene.endImageId ?? null,
takes: scene.takes || [],
});
// Serialize take mutations; a failure toasts and leaves the chain usable.
// Resolves true when the change landed, false when it was refused.
const runTakeOp = (work, failMessage) => {
const projectId = project.id;
const run = chainRef.current.then(() => work(projectId));
chainRef.current = run.then((scene) => { if (scene) applyScene(projectId, scene); return true; })
.catch((err) => { toast.error(err?.message || failMessage); return false; });
return chainRef.current;
};
const selectTake = (scene, take) => runTakeOp(
(projectId) => selectMusicVideoSceneTake(projectId, scene.sceneId, take.takeId, { silent: true }),
'Failed to select take',
);
// An unused still or clip from the project's pool becomes this shot's selection (with its partner, when picked).
// The still's frame prompt comes with it, so the start or end prompt shows what made the picture.
const placePoolItem = (scene, pick) => runTakeOp(
(projectId) => placeMusicVideoPoolItem(projectId, scene.sceneId, pick, { silent: true }).then(({ scene: placed }) => {
const field = pick.kind === 'endImage' ? 'endFramePrompt' : 'framePrompt';
const stillPlaced = pick.kind !== 'video' || Boolean(pick.withAssetId);
if (stillPlaced && placed[field] !== scene[field]) applyScenePatch?.(projectId, placed.sceneId, { [field]: placed[field] ?? null });
return placed;
}),
'Failed to use that take',
);
// "Start clip here": the shot's clip from a later frame becomes a new clip take, and the selected one.
// It keeps the cut clip's still and provider, so a still change still marks it stale and the pool pairs it.
const trimClipTake = (scene, startSec) => {
const origin = scene.takes?.find((take) => take.kind === 'video' && take.assetId === scene.videoHistoryId);
const provenance = {
...(origin?.sourceImageId ? { sourceImageId: origin.sourceImageId } : {}),
// Only a slug the take route accepts; an older free-form label is left off rather than failing the trim.
...(/^[a-z0-9][a-z0-9._-]{0,39}$/i.test(origin?.provider || '') ? { provider: origin.provider } : {}),
};
return runTakeOp(
(projectId) => trimVideoStart(scene.videoHistoryId, startSec, { silent: true })
.then(({ video }) => addMusicVideoSceneTake(projectId, scene.sceneId, { kind: 'video', assetId: video.id, source: 'trimmed', ...provenance }, { silent: true }))
.then(({ take }) => selectMusicVideoSceneTake(projectId, scene.sceneId, take.takeId, { silent: true })),
'Failed to trim the clip',
);
};
// Continuing shot: the previous shot's last on-screen frame becomes this
// scene's start frame; `keepStillAsEnd` keeps the current one as its end frame.
const continueFromPrevious = (scene, { keepStillAsEnd = false } = {}) => runTakeOp(
(projectId) => continueMusicVideoSceneFromPrevious(projectId, scene.sceneId, { keepStillAsEnd }, { silent: true }).then((res) => res.scene),
'Failed to continue from the previous shot',
);
const reviewTake = (scene, take, review) => runTakeOp(
(projectId) => reviewMusicVideoSceneTake(projectId, scene.sceneId, take.takeId, review, { silent: true }),
'Failed to update take',
);
// Mark a take's file bad (or clear it): a project-wide mark, separate from rejecting it from this shot.
// A mark's answer: the project's marks, and the shots whose takes a Bad mark rejected.
const applyPoolMarks = (projectId, { assetMarks, scenes = [] } = {}) => {
if (assetMarks) patchProject?.(projectId, { assetMarks });
for (const scene of scenes) applyScene(projectId, scene);
};
const markTake = (take, mark) => runTakeOp(
(projectId) => markMusicVideoPoolAssets(projectId, [{ kind: take.kind === 'video' ? 'video' : 'image', assetId: take.assetId }], mark, { silent: true })
.then((result) => { applyPoolMarks(projectId, result); return null; }),
'Failed to mark the take',
);
// Start and end frames trade places, each bringing its prompt along.
const swapFrames = (scene) => runTakeOp(
(projectId) => swapMusicVideoSceneFrames(projectId, scene.sceneId, { silent: true }).then((placed) => {
applyScenePatch?.(projectId, placed.sceneId, { framePrompt: placed.framePrompt ?? null, endFramePrompt: placed.endFramePrompt ?? null });
return placed;
}),
'Failed to swap the frames',
);
// Finalize a shot: every take other than its selected frame, end frame and clip is rejected (files kept).
const rejectUnusedTakes = (scene) => runTakeOp(
(projectId) => rejectUnusedMusicVideoSceneTakes(projectId, scene.sceneId, { silent: true }).then((res) => res.scene),
'Failed to reject the unused takes',
);
// A frame picked from (or uploaded into) the gallery for one scene.
const importTake = (scene, item) => runTakeOp(
(projectId) => addMusicVideoSceneTake(projectId, scene.sceneId, {
kind: 'image', assetId: item.filename, source: 'imported',
}, { silent: true }).then((res) => res.scene),
'Failed to import take',
);
// A clip picked from the video-history gallery for one scene (#8978). Same
// append-only contract as importTake: it fills the scene's video slot only
// while empty (appendSceneTakes), never replacing an existing selection.
const importClipTake = (scene, item) => runTakeOp(
(projectId) => addMusicVideoSceneTake(projectId, scene.sceneId, {
kind: 'video', assetId: item.id, source: 'imported',
}, { silent: true }).then((res) => res.scene),
'Failed to import clip take',
);
const exportHandoff = () => getMusicVideoHandoff(project.id, { silent: true })
.then((manifest) => {
downloadBlob(JSON.stringify(manifest, null, 2), handoffFilename(project.name), 'application/json');
toast.success(`Exported prompts for ${plural(manifest.scenes.length, 'scene')}`);
})
.catch((err) => toast.error(err?.message || 'Handoff export failed'));
// Downloadable ZIP counterpart (#8978): manifest.json plus every reference
// image and each scene's selected frame, so nothing has to be saved out of
// the gallery by hand before attaching it in the external tool.
const exportHandoffBundle = () => getMusicVideoHandoffBundle(project.id, { silent: true })
.then((buffer) => {
downloadBlob(buffer, handoffBundleFilename(project.name), 'application/zip');
toast.success('Exported handoff bundle');
})
.catch((err) => toast.error(err?.message || 'Handoff bundle export failed'));
const importHandoffFiles = async (files, provider) => {
if (!files.length) return;
if (files.length > MAX_HANDOFF_FILES) {
toast.error(`Import at most ${MAX_HANDOFF_FILES} files at a time (picked ${files.length})`);
return;
}
const projectId = project.id;
setBusy(true);
const items = [];
const failures = [];
for (const file of files) {
const isVideo = typeof file.type === 'string' && file.type.startsWith('video/');
const invalid = handoffFileProblem(file, isVideo);
if (invalid) { failures.push(invalid); continue; }
// Sequential uploads keep disk and decode work bounded.
const saved = await (isVideo
? uploadGalleryVideo(file, file.name, { silent: true })
: uploadGalleryImage(file, { silent: true })
).catch((err) => { failures.push(`${file.name}: ${err?.message || 'upload failed'}`); return null; });
const assetId = isVideo ? saved?.id : saved?.filename;
if (assetId) items.push({ kind: isVideo ? 'video' : 'image', assetId, originalName: file.name });
}
if (items.length) {
await importMusicVideoHandoff(projectId, { provider, items }, { silent: true })
.then(({ project: next, imported, skipped }) => {
const touched = new Set(imported.map((i) => i.sceneId));
for (const scene of next.scenes || []) if (touched.has(scene.sceneId)) applyScene(projectId, scene);
if (imported.length) toast.success(`Imported ${plural(imported.length, 'take')} from ${provider}`);
if (skipped.length) {
toast.error(`${plural(skipped.length, 'file')} matched no scene tag — they're in the gallery; use "Import take" on a scene to place them`);
}
})
.catch((err) => failures.push(err?.message || 'Handoff import failed'));
}
setBusy(false);
if (failures.length) toast.error(failures.slice(0, 3).join(' · '));
};
return { busy, selectTake, placePoolItem, trimClipTake, reviewTake, rejectUnusedTakes, swapFrames, markTake, applyPoolMarks, continueFromPrevious, importTake, importClipTake, exportHandoff, exportHandoffBundle, importHandoffFiles };
}