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

26.2 KB
Raw Blame
/**
 * Editable configuration form for a Creative Commission (#2657).
 *
 * The pure field sections of a commission's brief/schedule/generation/assignment,
 * shared by the index create drawer and the routed detail page. Run history +
 * render previews are rendered separately (see RenderHistory.jsx) so this stays a
 * config-only surface — the create drawer has no runs, and the detail page shows
 * renders in a dedicated gallery above the config.
 *
 * State lives in the PARENT (per the Drawer state-hoisting rule); this component
 * is fully controlled via `form` + `patchForm`.
 */

import { useEffect, useMemo, useState } from 'react';
import ProviderModelSelector from '../ProviderModelSelector';
import { isProcessProvider } from '../../utils/providers';
import { getProviders, getSettings, listImageModels, listVideoModels, listMusicEngines } from '../../services/api';
import UniverseMoodBoardPicker, { useStyleSourceLists } from '../media/UniverseMoodBoardPicker';
import { deriveAvailableBackends } from '../../lib/imageGenBackends';
import CronSchedulePicker from '../CronSchedulePicker';
import useUserTimezone from '../../hooks/useUserTimezone.js';
import {
  inputCls, labelCls,
  ABILITY_OPTIONS, GENERATION_FIELDS_BY_ABILITY, mergeGenerationForAbility,
  backendFieldsForAbility, RENDER_BACKEND_AUTO,
  COMMISSION_NAME_MAX, COMMISSION_INTENT_MAX, COMMISSION_STYLE_SPEC_MAX, COMMISSION_BRIEF_TAG_MAX,
} from './commissionForm.js';

// Multi-line so the field reads as room for DIRECTION rather than a tagline.
const INTENT_PLACEHOLDER = `something surreal, dreamlike, unsettlingly beautiful

Describe the cause, not the effect: physics and emotion, not adjectives.
One causal beat per shot, in order. Diegetic audio only, no music.`;

export default function CommissionConfigForm({ form, patchForm, saving, onSave, onCancel, saveLabel = 'Save' }) {
  const userTimezone = useUserTimezone();

  return (
    <div className="space-y-5">
      {/* Identity */}
      <section className="space-y-3">
        <div>
          <label className={labelCls} htmlFor="commission-name">Name</label>
          <input
            id="commission-name"
            className={inputCls}
            value={form.name}
            maxLength={COMMISSION_NAME_MAX}
            onChange={(e) => patchForm(['name'], e.target.value)}
            placeholder="Nightly Surreal"
          />
        </div>
        <label className="flex items-center gap-2 text-sm text-gray-300">
          <input
            type="checkbox"
            checked={form.enabled}
            onChange={(e) => patchForm(['enabled'], e.target.checked)}
          />
          Enabled (fires on schedule)
        </label>
      </section>

      {/* Output type — drives which generation params + directive the run uses */}
      <section className="space-y-3 border-t border-port-border pt-4">
        <div>
          <label className={labelCls} htmlFor="commission-ability">Creative output</label>
          <select
            id="commission-ability"
            className={inputCls}
            value={form.targetAbility}
            onChange={(e) => {
              const next = e.target.value;
              // Re-seed the generation params for the new type (carrying over any
              // overlapping value) BEFORE switching the type, so the rendered
              // fields and the payload always match the selected output.
              patchForm(['generation'], mergeGenerationForAbility(next, form.generation));
              patchForm(['targetAbility'], next);
            }}
          >
            {ABILITY_OPTIONS.map((o) => <option key={o.id} value={o.id}>{o.label}</option>)}
          </select>
          <p className="text-xs text-gray-500 mt-1">
            What each scheduled run produces. The parameters and the Creative Director&apos;s brief adapt to this type.
          </p>
        </div>
      </section>

      {/* Brief */}
      <section className="space-y-3 border-t border-port-border pt-4">
        <h3 className="text-sm font-semibold text-gray-200">Brief</h3>
        <div>
          <label className={labelCls} htmlFor="commission-intent">Intent</label>
          <textarea
            id="commission-intent"
            className={`${inputCls} min-h-[180px] leading-relaxed`}
            value={form.brief.intent}
            maxLength={COMMISSION_INTENT_MAX}
            onChange={(e) => patchForm(['brief', 'intent'], e.target.value)}
            placeholder={INTENT_PLACEHOLDER}
          />
          <div className="flex items-start justify-between gap-3 mt-1">
            <p className="text-xs text-gray-500">
              What to make, and the standing craft direction for how to make it.
            </p>
            <span className="text-xs text-gray-500 shrink-0 tabular-nums">
              {form.brief.intent.length}/{COMMISSION_INTENT_MAX}
            </span>
          </div>
        </div>
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
          <div>
            <label className={labelCls} htmlFor="commission-genre">Genre (optional)</label>
            <input
              id="commission-genre"
              className={inputCls}
              value={form.brief.genre}
              maxLength={COMMISSION_BRIEF_TAG_MAX}
              onChange={(e) => patchForm(['brief', 'genre'], e.target.value)}
              placeholder="surrealism"
            />
          </div>
          <div>
            <label className={labelCls} htmlFor="commission-style">Style notes (optional)</label>
            <input
              id="commission-style"
              className={inputCls}
              value={form.brief.styleSpec}
              maxLength={COMMISSION_STYLE_SPEC_MAX}
              onChange={(e) => patchForm(['brief', 'styleSpec'], e.target.value)}
              placeholder="flat color, Magritte"
            />
          </div>
        </div>
      </section>

      {/* Style source — a universe / mood board as the art-direction base */}
      <StyleSourceSection styleSource={form.styleSource} patchForm={patchForm} />

      {/* Schedule */}
      <section className="space-y-3 border-t border-port-border pt-4">
        <h3 className="text-sm font-semibold text-gray-200">Schedule</h3>
        <CronSchedulePicker
          value={form.schedule}
          valueShape="commission"
          timezone={form.schedule.timezone || userTimezone}
          onChange={schedule => patchForm(['schedule'], schedule)}
        />
      </section>

      {/* Generation — fields adapt to the selected output type (#2769) */}
      <GenerationSection ability={form.targetAbility} generation={form.generation} patchForm={patchForm} />

      {form.targetAbility === 'music' && (
        <MusicTasteSection musicTaste={form.musicTaste} patchForm={patchForm} />
      )}

      {/* Render backend — which image/video backend actually renders (#3135) */}
      <RenderBackendSection ability={form.targetAbility} generation={form.generation} patchForm={patchForm} />

      {/* AI provider & model — who processes this commission */}
      <section className="space-y-2 border-t border-port-border pt-4">
        <h3 className="text-sm font-semibold text-gray-200">AI provider &amp; model</h3>
        <p className="text-xs text-gray-500">
          Which AI writes the treatment and production plan each time this commission runs. Leave on the
          default to use your install&apos;s configured Creative Director assignment.
        </p>
        <AssignmentPicker
          assignment={form.assignment}
          onChange={(next) => patchForm(['assignment'], next)}
          onEffortChange={(effort) => patchForm(['assignment', 'effort'], effort)}
        />
      </section>

      {/* Feedback conditioning */}
      <section className="space-y-2 border-t border-port-border pt-4">
        <h3 className="text-sm font-semibold text-gray-200">Feedback conditioning</h3>
        <div className="flex items-center gap-3">
          <label className={`${labelCls} mb-0`} htmlFor="commission-feedback-window">Recent reactions to steer by</label>
          <input
            id="commission-feedback-window"
            type="number"
            min={0}
            max={50}
            className={`${inputCls} w-20`}
            value={form.feedbackWindow}
            onChange={(e) => patchForm(['feedbackWindow'], e.target.value)}
          />
        </div>
        <p className="text-xs text-gray-500">
          The last N ratings + notes are folded into the next run&apos;s brief. 0 disables conditioning.
        </p>
      </section>

      <div className="flex items-center gap-2 border-t border-port-border pt-4">
        <button
          onClick={onSave}
          disabled={saving}
          className="bg-port-accent hover:bg-blue-600 disabled:opacity-50 text-white px-4 py-2 rounded text-sm font-medium"
        >
          {saving ? 'Saving…' : saveLabel}
        </button>
        {onCancel && (
          <button onClick={onCancel} className="text-gray-400 hover:text-gray-200 px-4 py-2 text-sm">Cancel</button>
        )}
      </div>
    </div>
  );
}

// The universe and/or mood board a commission draws its look from — the same
// pickers Code Animation offers. Each run resolves them fresh on the server
// (services/creativeCommissions/styleSource.js): the universe's style tags,
// notes, and style images plus the board's through-line and pins become the
// art-direction base the Creative Director works from; the Style notes above
// refine it.
function StyleSourceSection({ styleSource, patchForm }) {
  const lists = useStyleSourceLists();
  return (
    <section className="space-y-3 border-t border-port-border pt-4">
      <h3 className="text-sm font-semibold text-gray-200">Style source</h3>
      <p className="text-xs text-gray-500">
        Pick a universe or mood board and each run loads its style tags and reference images as the
        Creative Director&apos;s art-direction base. Style notes above refine it.
      </p>
      <UniverseMoodBoardPicker
        lists={lists}
        idPrefix="commission"
        labelClass={labelCls}
        inputClass={inputCls}
        universeId={styleSource.universeId}
        moodBoardChoice={styleSource.moodBoardChoice}
        onUniverseChange={(value) => patchForm(['styleSource', 'universeId'], value)}
        onBoardChoiceChange={(value) => patchForm(['styleSource', 'moodBoardChoice'], value)}
      />
    </section>
  );
}

// Generation params for the selected output type (#2769). Renders the ability's
// field descriptors (GENERATION_FIELDS_BY_ABILITY) generically so all five types
// share one layout — a select for enum fields, a bounded number input otherwise.
// The label names the type so it's clear which output these knobs drive.
function GenerationSection({ ability, generation, patchForm }) {
  // Backend pins (#3135) are rendered by RenderBackendSection — they need a
  // conditional model picker and live availability data the generic grid has no
  // room for, so they're excluded here rather than squeezed into a bare select.
  const fields = (GENERATION_FIELDS_BY_ABILITY[ability] || GENERATION_FIELDS_BY_ABILITY.video)
    .filter((f) => f.type !== 'backend');
  const abilityLabel = (ABILITY_OPTIONS.find((o) => o.id === ability) || ABILITY_OPTIONS[0]).label;
  return (
    <section className="space-y-3 border-t border-port-border pt-4">
      <h3 className="text-sm font-semibold text-gray-200">Generation ({abilityLabel})</h3>
      <div className="grid grid-cols-1 sm:grid-cols-3 gap-3">
        {fields.map((field) => {
          if (field.key === 'targetDurationSeconds' && generation?.durationMode === 'auto') return null;
          const id = `commission-gen-${field.key}`;
          const value = generation?.[field.key] ?? '';
          return (
            <div key={field.key}>
              <label className={labelCls} htmlFor={id}>{field.label}</label>
              {field.type === 'select' ? (
                <select
                  id={id}
                  className={inputCls}
                  value={value}
                  onChange={(e) => patchForm(['generation', field.key], e.target.value)}
                >
                  {field.options.map(([val, lbl]) => <option key={val} value={val}>{lbl}</option>)}
                </select>
              ) : (
                <input
                  id={id}
                  type="number"
                  min={field.min}
                  max={field.max}
                  className={inputCls}
                  value={value}
                  onChange={(e) => patchForm(['generation', field.key], e.target.value)}
                />
              )}
              {field.key === 'durationMode' && value === 'auto' && (
                <p className="text-xs text-gray-500 mt-1">The Creative Director selects a suitable length for each commission.</p>
              )}
            </div>
          );
        })}
      </div>
    </section>
  );
}

function MusicTasteSection({ musicTaste, patchForm }) {
  const [catalog, setCatalog] = useState(null);

  useEffect(() => {
    let cancelled = false;
    listMusicEngines({ silent: true })
      .then((value) => { if (!cancelled) setCatalog(value); })
      .catch(() => { if (!cancelled) setCatalog({ engines: [], defaultEngine: null, error: true }); });
    return () => { cancelled = true; };
  }, []);

  const selectedEngineId = musicTaste.musicEngineId || catalog?.defaultEngine || '';
  const selectedEngine = catalog?.engines?.find((engine) => engine.id === selectedEngineId) || null;
  let readiness = null;
  if (catalog?.error) readiness = 'Music engine readiness could not be checked. Open Music → Generate before enabling this commission.';
  else if (catalog && !selectedEngine) readiness = 'The configured music engine is no longer available. Choose an installed engine before enabling this commission.';
  else if (selectedEngine && !selectedEngine.platformSupported) readiness = `${selectedEngine.name} requires ${selectedEngine.platformLabel} on this machine.`;
  else if (selectedEngine?.cudaRequired && selectedEngine.cudaState !== 'available') readiness = `${selectedEngine.name} requires an available NVIDIA CUDA GPU.`;
  else if (selectedEngine && !selectedEngine.runtimeReady) readiness = `${selectedEngine.name} is not installed. Install its runtime from Music → Generate.`;
  else if (selectedEngine && !selectedEngine.modelReady) readiness = `${selectedEngine.name} model weights are not installed. Install them from Music → Generate.`;
  else if (musicTaste.musicModelId && !selectedEngine?.models?.some((model) => model.id === musicTaste.musicModelId)) readiness = 'The configured music model is no longer available. Choose an installed model before enabling this commission.';

  return (
    <section className="space-y-3 border-t border-port-border pt-4">
      <h3 className="text-sm font-semibold text-gray-200">Digital Twin taste exploration</h3>
      <label className="flex items-center gap-2 text-sm text-gray-300" htmlFor="commission-music-taste-enabled">
        <input
          id="commission-music-taste-enabled"
          type="checkbox"
          checked={musicTaste.enabled}
          onChange={(e) => patchForm(['musicTaste', 'enabled'], e.target.checked)}
        />
        Use my Digital Twin music taste for each scheduled original track
      </label>
      <p className="text-xs text-gray-500">
        Each run combines bounded listening anchors with a controlled amount of exploration. Raw listening history stays on this machine.
      </p>
      {musicTaste.enabled && (
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
          <div>
            <label className={labelCls} htmlFor="commission-music-taste-source">Taste source</label>
            <select id="commission-music-taste-source" className={inputCls} value="digital-twin" disabled>
              <option value="digital-twin">Digital Twin profile</option>
            </select>
          </div>
          <div>
            <label className={labelCls} htmlFor="commission-music-taste-window">Listening window</label>
            <select
              id="commission-music-taste-window"
              className={inputCls}
              value={musicTaste.window}
              onChange={(e) => patchForm(['musicTaste', 'window'], e.target.value)}
            >
              <option value="week">Past week</option>
              <option value="month">Past month</option>
            </select>
          </div>
          <div>
            <label className={labelCls} htmlFor="commission-music-taste-anchors">Anchors per run</label>
            <input
              id="commission-music-taste-anchors"
              type="number"
              min={1}
              max={5}
              className={inputCls}
              value={musicTaste.anchorCount}
              onChange={(e) => patchForm(['musicTaste', 'anchorCount'], e.target.value)}
            />
          </div>
          <div>
            <label className={labelCls} htmlFor="commission-music-taste-exploration">Exploration (%)</label>
            <input
              id="commission-music-taste-exploration"
              type="number"
              min={0}
              max={100}
              className={inputCls}
              value={musicTaste.explorationPercent}
              onChange={(e) => patchForm(['musicTaste', 'explorationPercent'], e.target.value)}
            />
          </div>
          <div>
            <label className={labelCls} htmlFor="commission-music-taste-engine">Music engine</label>
            <select
              id="commission-music-taste-engine"
              className={inputCls}
              value={musicTaste.musicEngineId}
              onChange={(e) => {
                patchForm(['musicTaste', 'musicEngineId'], e.target.value);
                patchForm(['musicTaste', 'musicModelId'], '');
              }}
            >
              <option value="">Default music engine</option>
              {(catalog?.engines || []).map((engine) => <option key={engine.id} value={engine.id}>{engine.name}</option>)}
            </select>
          </div>
          <div>
            <label className={labelCls} htmlFor="commission-music-taste-model">Music model</label>
            <select
              id="commission-music-taste-model"
              className={inputCls}
              value={musicTaste.musicModelId}
              onChange={(e) => patchForm(['musicTaste', 'musicModelId'], e.target.value)}
            >
              <option value="">Default model{selectedEngine?.defaultModelId ? ` (${selectedEngine.defaultModelId})` : ''}</option>
              {(selectedEngine?.models || []).map((model) => <option key={model.id} value={model.id}>{model.name || model.id}</option>)}
            </select>
          </div>
        </div>
      )}
      {musicTaste.enabled && readiness && <p className="text-xs text-port-warning">{readiness}</p>}
    </section>
  );
}

// Which backend a mode's model picker reads its catalog from.
const MODEL_LOADERS = { image: listImageModels, video: listVideoModels };

/**
 * Render-backend pin for the selected output type (#3135) — a mode selector plus
 * a conditional model picker, mirroring the UX already shipped for pipeline
 * visual stages (VisualGenSettings.jsx). Rendered only for abilities that
 * actually enqueue that kind of render (image / video / music-video); `music` and
 * `series` declare no backend fields, so the whole section disappears.
 *
 * 'Auto' is the default and a true no-op: the scheduled fire resolves the
 * install-wide default exactly as it did before pins existed.
 */
function RenderBackendSection({ ability, generation, patchForm }) {
  const fields = backendFieldsForAbility(ability);
  const [settings, setSettings] = useState(null);
  const [models, setModels] = useState({ image: [], video: [] });

  useEffect(() => {
    let cancelled = false;
    // Silent: an unavailable list degrades to "Auto only + a Default model
    // option", which is still a usable form — no toast for that.
    getSettings({ silent: true })
      .then((s) => { if (!cancelled) setSettings(s); })
      .catch(() => { /* availability hints omitted */ });
    return () => { cancelled = true; };
  }, []);

  // Load a catalog per model-bearing kind actually on screen, so a music/series
  // commission fetches nothing and an image commission never pulls the video list.
  const modelKinds = fields.map((f) => f.modelKind).join(',');
  useEffect(() => {
    let cancelled = false;
    for (const kind of modelKinds ? modelKinds.split(',') : []) {
      MODEL_LOADERS[kind]?.({ silent: true })
        .then((list) => {
          if (!cancelled) setModels((prev) => ({ ...prev, [kind]: Array.isArray(list) ? list : [] }));
        })
        .catch(() => { /* picker falls back to the default-only option */ });
    }
    return () => { cancelled = true; };
  }, [modelKinds]);

  // Which backends this install can actually render on. Used to warn (not to
  // hide) — a pin for a currently-disabled backend must stay visible and editable,
  // otherwise the user can't tell why their choice stopped applying.
  const availableIds = useMemo(
    () => new Set(deriveAvailableBackends(settings, { excludeExternal: true }).map((b) => b.id)),
    [settings],
  );

  if (fields.length === 0) return null;

  return (
    <section className="space-y-3 border-t border-port-border pt-4">
      <h3 className="text-sm font-semibold text-gray-200">Render backend</h3>
      <p className="text-xs text-gray-500">
        Which backend actually renders each run. Leave on <strong className="text-gray-400">Auto</strong> to
        follow your install&apos;s Settings → Image Gen defaults. Pin{' '}
        <strong className="text-gray-400">Local</strong> to also choose the model each render uses; the cloud
        backends pick their own.
      </p>
      <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
        {fields.map((field) => {
          const modeId = `commission-gen-${field.key}`;
          const modelId = `commission-gen-${field.modelKey}`;
          const mode = generation?.[field.key] || RENDER_BACKEND_AUTO;
          const showModel = field.modelModes.includes(mode);
          const list = models[field.modelKind] || [];
          // Only warn about a cloud CLI the settings say is off. `local` is
          // reported unavailable purely for a missing pythonPath, which the
          // local-model picker below already makes obvious.
          const unavailable = mode !== RENDER_BACKEND_AUTO && mode !== 'local' && settings && !availableIds.has(mode);
          return (
            <div key={field.key} className="space-y-2">
              <div>
                <label className={labelCls} htmlFor={modeId}>{field.label}</label>
                <select
                  id={modeId}
                  className={inputCls}
                  value={mode}
                  onChange={(e) => {
                    const next = e.target.value;
                    patchForm(['generation', field.key], next);
                    // Drop a stale model id when moving to a backend that has no
                    // model knob, so the form never shows a pin that can't apply.
                    if (!field.modelModes.includes(next)) patchForm(['generation', field.modelKey], null);
                  }}
                >
                  {field.options.map(([val, lbl]) => <option key={val} value={val}>{lbl}</option>)}
                </select>
                {unavailable && (
                  <p className="text-xs text-port-warning mt-1">
                    This backend is disabled in Settings → Image Gen — runs fall back to the default until you enable it.
                  </p>
                )}
              </div>
              {showModel && (
                <div>
                  <label className={labelCls} htmlFor={modelId}>{field.modelLabel}</label>
                  <select
                    id={modelId}
                    className={inputCls}
                    value={generation?.[field.modelKey] || ''}
                    onChange={(e) => patchForm(['generation', field.modelKey], e.target.value || null)}
                  >
                    <option value="">Default ({list.find((m) => m.default)?.name || list[0]?.name || 'install default'})</option>
                    {list.map((m) => <option key={m.id} value={m.id}>{m.name || m.id}</option>)}
                  </select>
                </div>
              )}
            </div>
          );
        })}
      </div>
    </section>
  );
}

// AI provider/model picker for the commission's CD cognitive stages (treatment +
// plan). Mirrors SeriesLlmPicker: fetches the provider list, drives the shared
// ProviderModelSelector, and reports changes up to form state via `onChange`.
// Only agent-harness (CLI/TUI) providers are offered — an API-type provider
// injected into a CoS agent task trips the server's harness-boundary guard.
//
// The empty option deliberately does NOT name a specific provider: an unset pin
// resolves at fire time to `settings.creativeDirector.{treatment,plan}` (falling
// back to the active provider only when those stages are themselves unassigned),
// so naming the registry's active provider here would misreport the processor on
// installs that assign the CD stages separately. Label it neutrally; the section
// helper text points the user at their Creative Director assignment.
function AssignmentPicker({ assignment, onChange, onEffortChange }) {
  const [providers, setProviders] = useState([]);

  useEffect(() => {
    getProviders({ silent: true })
      .then((data) => setProviders((data?.providers || []).filter(isProcessProvider)))
      .catch(() => { /* dropdowns fall back to the "install default" option */ });
  }, []);

  const availableModels = useMemo(() => {
    const p = providers.find((x) => x.id === assignment.providerId);
    return p?.models || [];
  }, [providers, assignment.providerId]);

  return (
    <ProviderModelSelector
      providers={providers}
      selectedProviderId={assignment.providerId || ''}
      selectedModel={assignment.model || ''}
      effort={assignment.effort || ''}
      onEffortChange={onEffortChange}
      availableModels={availableModels}
      onProviderChange={(id) => onChange({ providerId: id || '', model: '', effort: '' })}
      onModelChange={(model) => onChange({ ...assignment, model: model || '' })}
      label="Provider"
      modelDisabled={availableModels.length === 0}
      alwaysShowModel
      highlightToolUse
      emptyProviderOption="Install default (Creative Director assignment)"
      emptyModelOption="Default model"
    />
  );
}