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

15.6 KB
Raw Blame
import CodeAuthoringPicker from './CodeAuthoringPicker.jsx';
import MediaModePicker from './MediaModePicker.jsx';
import AutoApproveFields from './AutoApproveFields.jsx';
import { useState } from 'react';
import { Wand2 } from 'lucide-react';
import Drawer from '../Drawer.jsx';
import ToggleChip from '../ui/ToggleChip.jsx';
import ToolPicker from './ToolPicker.jsx';
import MoodBoardPicker from './MoodBoardPicker.jsx';
import CharacterStyleSelect from './CharacterStyleSelect.jsx';
import SongSourcePicker from './SongSourcePicker.jsx';
import ExistingSongSelect from './ExistingSongSelect.jsx';
import ProviderModelSelector from '../ProviderModelSelector.jsx';
import MusicVideoLlmStagesPicker from './MusicVideoLlmStagesPicker.jsx';
import toast from '../ui/Toast';
import useProviderModels from '../../hooks/useProviderModels.js';
import { startAutonomousMusicVideo } from '../../services/apiMusicVideo.js';
import {
  AUTONOMOUS_CHECKPOINT_IDS, AUTONOMOUS_CHECKPOINT_LABELS, AUTONOMOUS_LIMIT_BOUNDS, ORCHESTRATOR_CHECKPOINTS, REUSED_SONG_SKIPPED_CHECKPOINTS, SUNO_LIMITS, SUNO_VOCAL_GENDERS, autonomousRequestFromDraft, emptyAutonomousDraft,
  isSunoModelValid,
} from '../../lib/musicVideoAutonomous.js';

const inputClass = 'w-full min-w-0 bg-port-bg border border-port-border rounded px-2 py-1.5 text-sm';

/**
 * "Autonomous music video" — the alternate entry point: one prompt, no track,
 * style or board picked up front. The server writes a creative brief, lyrics and
 * a mood board, makes the song (Suno in the PortOS Browser, or a local Music
 * Studio engine), then produces the video
 * with the chosen tools. Checkpoints are optional approval stops.
 */
export default function AutonomousStartDrawer({ open, onClose, onStarted }) {
  const [draft, setDraft] = useState(emptyAutonomousDraft);
  const [submitting, setSubmitting] = useState(false);
  const [authoringReady, setAuthoringReady] = useState(false);
  const llm = useProviderModels({ allowDefault: true, silent: true, withEffort: true });
  const [effort, setEffort] = useState('');
  // The orchestrator must name a provider, so its picker opens on the active one.
  const orchestratorLlm = useProviderModels({ silent: true, withEffort: true });
  const [orchestratorEffort, setOrchestratorEffort] = useState('');
  const orchestrated = draft.runMode === 'orchestrated';
  // An explicit per-run grant, authorized by the signed-in session.
  const [autoApprove, setAutoApprove] = useState([]);
  const [grantError, setGrantError] = useState(null);
  const patch = (next) => setDraft((d) => ({ ...d, ...next }));
  const toggleCheckpoint = (id) => patch({
    checkpoints: AUTONOMOUS_CHECKPOINT_IDS.filter((c) => (c === id ? !draft.checkpoints.includes(c) : draft.checkpoints.includes(c))),
  });
  const patchSuno = (next) => setDraft((d) => ({ ...d, suno: { ...d.suno, ...next } }));
  const reusingSong = !!draft.fromProjectId;
  const checkpointIds = reusingSong ? AUTONOMOUS_CHECKPOINT_IDS.filter((id) => !REUSED_SONG_SKIPPED_CHECKPOINTS.includes(id)) : AUTONOMOUS_CHECKPOINT_IDS;
  const sunoModelValid = reusingSong || draft.songSource !== 'suno' || isSunoModelValid(draft.suno.model);
  const valid = draft.prompt.trim().length > 0 && authoringReady && sunoModelValid && (!orchestrated || !!orchestratorLlm.selectedProviderId);

  const handleSubmit = (e) => {
    e.preventDefault();
    if (!valid || submitting) return;
    setSubmitting(true);
    setGrantError(null);
    startAutonomousMusicVideo(
      {
        ...autonomousRequestFromDraft({
          ...draft,
          orchestrator: { providerId: orchestratorLlm.selectedProviderId || '', model: orchestratorLlm.selectedModel || '', effort: orchestratorEffort },
        }, { providerId: llm.selectedProviderId || undefined, model: llm.selectedModel || undefined, effort: effort || undefined }),
        ...(!orchestrated && autoApprove.length ? { autoApprove } : {}),
      },
      { silent: true },
    )
      .then(({ project }) => {
        setDraft(emptyAutonomousDraft());
        setAutoApprove([]);
        toast.success('Autonomous music video started');
        onStarted(project);
      })
      .catch((err) => {
        if (err?.code === 'AUTH_REQUIRED') setGrantError(err.message);
        else toast.error(err?.message || 'Failed to start the autonomous music video');
      })
      .finally(() => setSubmitting(false));
  };

  return (
    <Drawer
      open={open}
      onClose={onClose}
      size="md"
      title="Autonomous music video"
      subtitle="One prompt in — lyrics, a song, visual direction and an authored video"
      closeOnEsc={false}
      closeOnBackdrop={false}
    >
      <form onSubmit={handleSubmit} className="space-y-4">
        <div>
          <label htmlFor="mv-auto-prompt" className="block text-xs text-port-text-muted mb-1">Prompt</label>
          <textarea
            id="mv-auto-prompt"
            rows={4}
            maxLength={4000}
            value={draft.prompt}
            onChange={(e) => patch({ prompt: e.target.value })}
            placeholder="A courier crosses a rainy neon city at night — melancholic synthwave, a hopeful chorus."
            className={inputClass}
          />
          <p className="text-[11px] text-port-text-muted mt-1">
            The agent develops visual direction from this prompt within your selected media mode.
          </p>
        </div>

        <ExistingSongSelect id="mv-auto-from-project" value={draft.fromProjectId} onChange={(fromProjectId) => patch({ fromProjectId })} />

        {!reusingSong && <SongSourcePicker idPrefix="mv-auto" songSource={draft.songSource} localFallback={draft.localFallback} localMusic={draft.localMusic} onChange={patch} />}

        {!reusingSong && draft.songSource === 'suno' && (
          <div className="grid grid-cols-[repeat(auto-fit,minmax(min(100%,10rem),16rem))] gap-3">
            <div>
              <label htmlFor="mv-auto-suno-exclude" className="block text-xs text-port-text-muted mb-1">Exclude styles</label>
              <input id="mv-auto-suno-exclude" type="text" maxLength={SUNO_LIMITS.excludeStyles} value={draft.suno.excludeStyles} onChange={(e) => patchSuno({ excludeStyles: e.target.value })} placeholder="metal, screamo" className={inputClass} />
            </div>
            {!draft.instrumental && (
              <div>
                <label htmlFor="mv-auto-suno-vocal" className="block text-xs text-port-text-muted mb-1">Vocal gender</label>
                <select id="mv-auto-suno-vocal" value={draft.suno.vocalGender} onChange={(e) => patchSuno({ vocalGender: e.target.value })} className={inputClass}>
                  <option value="">Suno decides</option>
                  {SUNO_VOCAL_GENDERS.map((gender) => <option key={gender} value={gender}>{gender === 'male' ? 'Male' : 'Female'}</option>)}
                </select>
              </div>
            )}
            <div>
              <label htmlFor="mv-auto-suno-model" className="block text-xs text-port-text-muted mb-1">Suno model</label>
              <input id="mv-auto-suno-model" type="text" maxLength={10} value={draft.suno.model} onChange={(e) => patchSuno({ model: e.target.value })} placeholder="Current (e.g. v6)" aria-invalid={!sunoModelValid} className={inputClass} />
              {!sunoModelValid && <p className="text-[11px] text-port-error mt-1">Use a version such as v6 or v6-wild.</p>}
            </div>
            <div>
              <label htmlFor="mv-auto-suno-max" className="block text-xs text-port-text-muted mb-1">Max Mode</label>
              <select id="mv-auto-suno-max" value={draft.suno.maxMode} onChange={(e) => patchSuno({ maxMode: e.target.value })} className={inputClass}>
                <option value="">Suno decides</option>
                <option value="on">On (higher quality, more credits)</option>
                <option value="off">Off</option>
              </select>
            </div>
          </div>
        )}

        {!reusingSong && <ToggleChip id="mv-auto-instrumental" label="Instrumental (no vocals)" checked={draft.instrumental} onToggle={() => patch({ instrumental: !draft.instrumental })} />}

        <MediaModePicker id="mv-auto-media-mode" value={draft.mediaMode} onChange={(mediaMode) => patch({ mediaMode })} />
        <CodeAuthoringPicker value={draft.authoring} onChange={(authoring) => patch({ authoring })} onValidityChange={setAuthoringReady} disabled={submitting} />
        <ToolPicker tools={draft.tools} models={draft.models} onChange={patch} />

        <CharacterStyleSelect id="mv-auto-character-style" value={draft.characterStyleId} onChange={(characterStyleId) => patch({ characterStyleId })} />
        <MoodBoardPicker id="mv-auto-mood-board" value={draft.moodBoardId} onChange={(moodBoardId) => patch({ moodBoardId })} />

        <div>
          <label htmlFor="mv-auto-guidance" className="block text-xs text-port-text-muted mb-1">Guidance (optional)</label>
          <textarea
            id="mv-auto-guidance"
            rows={2}
            maxLength={4000}
            value={draft.guidance}
            onChange={(e) => patch({ guidance: e.target.value })}
            placeholder="Tone, pacing, what to avoid — the agent writes and plans against this."
            className={inputClass}
          />
        </div>

        <div className="grid grid-cols-[repeat(auto-fit,minmax(min(100%,12rem),16rem))] gap-3">
          <div>
            <label htmlFor="mv-auto-budget" className="block text-xs text-port-text-muted mb-1">Video generation budget (USD)</label>
            <input id="mv-auto-budget" type="number" min="0" step="1" value={draft.budget} onChange={(e) => patch({ budget: e.target.value })} placeholder="No cap" aria-describedby="mv-auto-budget-hint" className={inputClass} />
            <p id="mv-auto-budget-hint" className="text-[11px] text-port-text-muted mt-1">Caps paid video and image generation only. Suno credits and LLM usage are separate.</p>
          </div>
          <div>
            <label htmlFor="mv-auto-generations" className="block text-xs text-port-text-muted mb-1">Max generations</label>
            <input id="mv-auto-generations" type="number" min="1" max="500" step="1" value={draft.maxGenerations} onChange={(e) => patch({ maxGenerations: e.target.value })} className={inputClass} />
          </div>
        </div>

        <div>
          <label htmlFor="mv-auto-run-mode" className="block text-xs text-port-text-muted mb-1">Who reviews each step</label>
          <select id="mv-auto-run-mode" value={draft.runMode} onChange={(e) => { patch({ runMode: e.target.value }); setGrantError(null); }} className={inputClass}>
            <option value="checkpoints">Me: pause where I choose</option>
            <option value="orchestrated">Orchestrator: an AI director reviews every step</option>
          </select>
        </div>

        <ToggleChip id="mv-auto-stop-before-video" label="Stop before video: stills first" checked={draft.stopBeforeVideo} onToggle={() => patch({ stopBeforeVideo: !draft.stopBeforeVideo })} />

        {orchestrated ? (
          <fieldset className="min-w-0 space-y-2" aria-labelledby="mv-auto-orchestrator-label">
            <span id="mv-auto-orchestrator-label" className="block text-xs text-port-text-muted">Orchestrator</span>
            <ProviderModelSelector
              providers={orchestratorLlm.providers}
              selectedProviderId={orchestratorLlm.selectedProviderId}
              selectedModel={orchestratorLlm.selectedModel}
              availableModels={orchestratorLlm.availableModels}
              onProviderChange={(id) => { orchestratorLlm.setSelectedProviderId(id); setOrchestratorEffort(''); }}
              onModelChange={orchestratorLlm.setSelectedModel}
              effort={orchestratorEffort}
              onEffortChange={setOrchestratorEffort}
              emptyModelOption="Provider default"
              label="Provider, model and effort"
              disabled={submitting}
              modelDisabled={orchestratorLlm.availableModels.length === 0}
              compact
            />
            <div className="max-w-[16rem]">
              <label htmlFor="mv-auto-review-attempts" className="block text-xs text-port-text-muted mb-1">Revisions per step</label>
              <input
                id="mv-auto-review-attempts"
                type="number"
                min={AUTONOMOUS_LIMIT_BOUNDS.maxReviewAttempts.min}
                max={AUTONOMOUS_LIMIT_BOUNDS.maxReviewAttempts.max}
                step="1"
                value={draft.maxReviewAttempts}
                onChange={(e) => patch({ maxReviewAttempts: e.target.value })}
                className={inputClass}
              />
            </div>
            <p className="text-[11px] text-port-text-muted">
              It judges {ORCHESTRATOR_CHECKPOINTS.map((c) => c.label.toLowerCase()).join(', ')}, approves each or sends it back, and approves planning stages with your signed-in session. A vision model can also look at the art sheet and the final video. It never publishes.
            </p>
            {grantError && <p role="alert" className="text-[11px] text-port-error break-words">{grantError}</p>}
          </fieldset>
        ) : (
          <>
            <fieldset className="min-w-0" aria-labelledby="mv-auto-checkpoints-label">
              <span id="mv-auto-checkpoints-label" className="block text-xs text-port-text-muted mb-1">Pause for my approval after (optional)</span>
              <div className="flex flex-wrap gap-1.5">
                {checkpointIds.map((id) => (
                  <ToggleChip
                    key={id}
                    id={`mv-auto-checkpoint-${id}`}
                    label={AUTONOMOUS_CHECKPOINT_LABELS[id]}
                    checked={draft.checkpoints.includes(id)}
                    onToggle={() => toggleCheckpoint(id)}
                  />
                ))}
              </div>
              <p className="text-[11px] text-port-text-muted mt-1">Select automatic planning approvals below if desired. The animated proof always needs a recorded review.</p>
            </fieldset>

            <AutoApproveFields
              idPrefix="mv-auto"
              value={autoApprove}
              onChange={(next) => { setAutoApprove(next); setGrantError(null); }}
              error={grantError}
            />
          </>
        )}

        {llm.providers.length > 0 && (
          <ProviderModelSelector
            providers={llm.providers}
            selectedProviderId={llm.selectedProviderId}
            selectedModel={llm.selectedModel}
            availableModels={llm.availableModels}
            onProviderChange={(id) => { llm.setSelectedProviderId(id); setEffort(''); }}
            onModelChange={llm.setSelectedModel}
            effort={effort}
            onEffortChange={setEffort}
            emptyProviderOption="Auto — a TUI provider when one is eligible"
            emptyModelOption="Provider default"
            label="Direction LLM (brief, lyrics and every stage on Default)"
            disabled={submitting}
            modelDisabled={llm.availableModels.length === 0}
            compact
          />
        )}

        <MusicVideoLlmStagesPicker
          idPrefix="mv-auto-start"
          value={draft.llmStages}
          onChange={(llmStages) => patch({ llmStages })}
          lyricsReview={draft.lyricsReview}
          onLyricsReviewChange={(lyricsReview) => patch({ lyricsReview })}
          disabled={submitting}
        />

        <button
          type="submit"
          disabled={!valid || submitting}
          className="w-full flex items-center justify-center gap-1 bg-port-accent text-white rounded px-2 py-1.5 text-sm min-h-[44px] sm:min-h-0 disabled:opacity-50"
        >
          <Wand2 size={16} /> {submitting ? 'Starting…' : 'Start autonomous video'}
        </button>
      </form>
    </Drawer>
  );
}