import { ExternalLinkIcon, NetworkIcon } from "lucide-react"; import { type ReactElement } from "react"; import { cn } from "~/lib/utils"; import { formatWorkflowDuration, formatWorkflowTokens, isRemoteWorkflowRun, type WorkflowRun, type WorkflowRunAgent, workflowRunTitle, } from "~/workflow-logic"; import { Button } from "../ui/button"; import { AgentRowContent, PhaseHeader, WorkflowStatusChip, agentRollupLabel, safeWorkflowSessionUrl, } from "./workflowUi"; const MAX_CARD_AGENT_ROWS = 8; function agentRecency(agent: WorkflowRunAgent): number { return agent.lastProgressAt ?? agent.startedAt ?? agent.queuedAt ?? 0; } /** Choose which agent indices survive the card cap: running+error first, then most recent. */ function selectVisibleAgentIndices(agents: WorkflowRunAgent[], cap: number): Set { const prioritized = [...agents].sort((a, b) => { const aUrgent = a.status === "running" || a.status === "error" ? 0 : 1; const bUrgent = b.status === "running" || b.status === "error" ? 0 : 1; if (aUrgent !== bUrgent) { return aUrgent - bUrgent; } return agentRecency(b) - agentRecency(a); }); return new Set(prioritized.slice(0, cap).map((agent) => agent.index)); } export function WorkflowRunCard(props: { workflowRun: WorkflowRun; onOpenDetails?: (() => void) | undefined; onStop?: (() => void) | undefined; }): ReactElement { const { workflowRun: run, onOpenDetails, onStop } = props; const title = workflowRunTitle(run); const remote = isRemoteWorkflowRun(run); const tokens = run.usage?.totalTokens; const durationMs = run.usage?.durationMs; const allAgents = run.phases.flatMap((phase) => phase.agents); const overCap = allAgents.length > MAX_CARD_AGENT_ROWS; const visibleIndices = overCap ? selectVisibleAgentIndices(allAgents, MAX_CARD_AGENT_ROWS) : null; const hiddenCount = overCap ? allAgents.length - MAX_CARD_AGENT_ROWS : 0; return (
{title}
{agentRollupLabel(run.agentCounts)} {tokens !== undefined && {formatWorkflowTokens(tokens)}} {durationMs !== undefined && {formatWorkflowDuration(durationMs)}}
{(onStop || onOpenDetails) && (
{onStop && ( )} {onOpenDetails && ( )}
)}
{remote ? ( ) : allAgents.length > 0 ? (
{run.phases.map((phase) => { const rows = phase.agents.filter( (agent) => visibleIndices === null || visibleIndices.has(agent.index), ); if (rows.length === 0) { return null; } return (
{rows.map((agent) => (
))}
); })} {hiddenCount > 0 && ( )}
) : null} {run.handles?.warning !== undefined && (

{run.handles.warning}

)}
); } function RemoteRunBody({ run }: { run: WorkflowRun }): ReactElement { const sessionUrl = safeWorkflowSessionUrl(run.handles?.sessionUrl); if (sessionUrl === undefined) { return (

Running in the cloud

); } return ( Running in the cloud — open session ); }