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

5.9 KB
Raw Blame
import { Link, useParams } from 'react-router';
import { Bot } from 'lucide-react';
import { extractKind } from './ActiveAgentsBanner.jsx';
import ScenePreview from './ScenePreview.jsx';
import { SCENE_STATUS_BADGE } from './sceneStatus.js';
import VideoShotEditor from './VideoShotEditor.jsx';

export default function SegmentsTab({ project, activeAgents = [], basePath = '/creative-director', onChange }) {
  const { sceneId: selectedSceneId } = useParams();
  const scenes = project.treatment?.scenes;
  if (!scenes?.length) {
    return <div className="text-port-text-muted text-sm">No scenes yet — the treatment hasn't been generated.</div>;
  }
  const sorted = scenes.slice().sort((a, b) => a.order - b.order);

  // Scene rendering runs server-side now (no `scene` CoS task), so the only
  // CD agent we'd see attached to a specific scene is `evaluate` — it reads
  // the rendered thumbnail and judges it against the project's style spec.
  // Use the explicit sceneId from the agent's task metadata (set by
  // agentBridge#enqueueEvaluateTask) when available — that pins the active
  // agent to the exact scene being judged. Fall back to the orchestrator's
  // "next non-terminal scene" guess only when metadata is missing.
  const sceneAgents = activeAgents.filter((a) => extractKind(a.taskId) === 'evaluate');
  const inflightScene = sorted.find((s) => s.status === 'pending' || s.status === 'rendering' || s.status === 'evaluating');
  const agentSceneIds = new Set(
    sceneAgents
      .map((a) => a.task?.metadata?.creativeDirector?.sceneId)
      .filter(Boolean),
  );
  const fallbackInflightSceneId = sceneAgents.length ? inflightScene?.sceneId : null;
  const isSceneInflight = (sceneId) =>
    agentSceneIds.has(sceneId) || (agentSceneIds.size === 0 && sceneId === fallbackInflightSceneId);
  // Pick out only the agents whose task metadata names this exact scene.
  // Without this filter the row would render every evaluate agent in the
  // project — confusing if multiple evaluates run concurrently or if an
  // earlier agent's metadata is stale. When metadata is missing for all
  // agents (older runs / queued pre-spawn), fall back to the orchestrator's
  // single inflight guess so the row still shows *something*.
  const agentsForScene = (sceneId) => {
    const tagged = sceneAgents.filter((a) => a.task?.metadata?.creativeDirector?.sceneId === sceneId);
    if (tagged.length) return tagged;
    if (agentSceneIds.size === 0 && sceneId === fallbackInflightSceneId) return sceneAgents;
    return [];
  };

  if (selectedSceneId && !sorted.some(s => s.sceneId === selectedSceneId)) return <div>Shot not found. <Link to={`${basePath}/${project.id}/segments`}>View all shots</Link></div>;
  return (
    <div>
      {selectedSceneId && <Link to={`${basePath}/${project.id}/segments`} className="block mb-3 text-port-accent">View all shots</Link>}
    <div className={selectedSceneId ? 'max-w-4xl' : 'grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3'}>
      {sorted.filter(s => !selectedSceneId || s.sceneId === selectedSceneId).map((s) => {
        const isInflight = isSceneInflight(s.sceneId);
        const decoratedStatus = isInflight && s.status === 'pending' ? 'rendering' : s.status;
        return (
          <div key={s.sceneId} className={`bg-port-card border rounded overflow-hidden ${isInflight ? 'border-port-accent/60' : 'border-port-border'}`}>
            {s.renderedJobId ? (
              <ScenePreview jobId={s.renderedJobId} label={`Scene ${s.order + 1}`} />
            ) : s.sourceImageFile ? (
              <img src={`/data/images/${encodeURIComponent(s.sourceImageFile)}`} alt={`Scene ${s.order + 1} reference frame`} className="aspect-video w-full object-contain bg-port-bg" />
            ) : (
              <div className="bg-port-bg aspect-video flex items-center justify-center text-port-text-muted text-xs">
                {isInflight ? (
                  <span className="text-port-accent flex items-center gap-1">
                    <Bot className="w-3 h-3 animate-pulse" /> agent working…
                  </span>
                ) : decoratedStatus === 'rendering' || decoratedStatus === 'evaluating' ? 'rendering…' : 'no render yet'}
              </div>
            )}
            <div className="p-2 space-y-1">
              <div className="flex items-center justify-between gap-2">
                <Link className="text-sm font-medium hover:underline" to={`${basePath}/${project.id}/segments/${encodeURIComponent(s.sceneId)}`}>Scene {s.order + 1}</Link>
                <span className={`text-xs px-2 py-0.5 rounded ${SCENE_STATUS_BADGE[decoratedStatus] || ''}`}>{decoratedStatus}</span>
              </div>
              <div className="text-xs text-port-text-muted truncate" title={s.intent}>{s.intent}</div>
              <div className="text-xs text-port-text-muted">
                {s.durationSeconds}s • retries: {s.retryCount || 0}
              </div>
              {isInflight && (() => {
                const myAgents = agentsForScene(s.sceneId);
                if (!myAgents.length) return null;
                return (
                  <div className="text-xs text-port-accent font-mono mt-1 flex flex-wrap gap-x-2">
                    {myAgents.map((a) => (
                      <Link key={a.id} to={`/cos/agents?id=${encodeURIComponent(a.id)}`} className="hover:underline">
                        {a.id?.slice(0, 14) || 'agent'}
                      </Link>
                    ))}
                  </div>
                );
              })()}
              {s.evaluation?.notes && (
                <div className="text-xs text-port-text-muted italic truncate" title={s.evaluation.notes}>
                  {s.evaluation.notes}
                </div>
              )}
              {selectedSceneId && project.workspace === 'video' && <VideoShotEditor key={`${s.sceneId}:${s.workRevision || 0}`} project={project} scene={s} onChange={onChange} />}
            </div>
          </div>
        );
      })}
    </div>
    </div>
  );
}