Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkflowRunCard.tsx

152 lines5.4 KB

1import { ExternalLinkIcon, NetworkIcon } from "lucide-react";
2import { type ReactElement } from "react";
3​
4import { cn } from "~/lib/utils";
5import {
6 formatWorkflowDuration,
7 formatWorkflowTokens,
8 isRemoteWorkflowRun,
9 type WorkflowRun,
10 type WorkflowRunAgent,
11 workflowRunTitle,
12} from "~/workflow-logic";
13import { Button } from "../ui/button";
14import {
15 AgentRowContent,
16 PhaseHeader,
17 WorkflowStatusChip,
18 agentRollupLabel,
19 safeWorkflowSessionUrl,
20} from "./workflowUi";
21​
22const MAX_CARD_AGENT_ROWS = 8;
23​
24function agentRecency(agent: WorkflowRunAgent): number {
25 return agent.lastProgressAt ?? agent.startedAt ?? agent.queuedAt ?? 0;
27​
28/** Choose which agent indices survive the card cap: running+error first, then most recent. */
29function selectVisibleAgentIndices(agents: WorkflowRunAgent[], cap: number): Set<number> {
30 const prioritized = [...agents].sort((a, b) => {
31 const aUrgent = a.status === "running" || a.status === "error" ? 0 : 1;
32 const bUrgent = b.status === "running" || b.status === "error" ? 0 : 1;
33 if (aUrgent !== bUrgent) {
34 return aUrgent - bUrgent;
35 }
36 return agentRecency(b) - agentRecency(a);
37 });
38 return new Set(prioritized.slice(0, cap).map((agent) => agent.index));
40​
41export function WorkflowRunCard(props: {
42 workflowRun: WorkflowRun;
43 onOpenDetails?: (() => void) | undefined;
44 onStop?: (() => void) | undefined;
45}): ReactElement {
46 const { workflowRun: run, onOpenDetails, onStop } = props;
47 const title = workflowRunTitle(run);
48 const remote = isRemoteWorkflowRun(run);
49 const tokens = run.usage?.totalTokens;
50 const durationMs = run.usage?.durationMs;
51​
52 const allAgents = run.phases.flatMap((phase) => phase.agents);
53 const overCap = allAgents.length > MAX_CARD_AGENT_ROWS;
54 const visibleIndices = overCap ? selectVisibleAgentIndices(allAgents, MAX_CARD_AGENT_ROWS) : null;
55 const hiddenCount = overCap ? allAgents.length - MAX_CARD_AGENT_ROWS : 0;
56​
57 return (
58 <div className="mt-2 rounded-lg border border-border/80 bg-card/45 p-2.5">
59 <div className="flex items-center gap-2">
60 <NetworkIcon className="size-3.5 shrink-0 text-muted-foreground" />
61 <span className="truncate font-medium text-[13px] text-foreground">{title}</span>
62 <WorkflowStatusChip status={run.status} />
63 <div className="ml-auto flex items-center gap-2">
64 <div className="flex items-center gap-2 text-[11px] text-muted-foreground/70 tabular-nums">
65 <span>{agentRollupLabel(run.agentCounts)}</span>
66 {tokens !== undefined && <span>{formatWorkflowTokens(tokens)}</span>}
67 {durationMs !== undefined && <span>{formatWorkflowDuration(durationMs)}</span>}
68 </div>
69 {(onStop || onOpenDetails) && (
70 <div className="flex items-center gap-1.5">
71 {onStop && (
72 <Button type="button" size="xs" variant="outline" onClick={onStop}>
73 Stop
74 </Button>
75 )}
76 {onOpenDetails && (
77 <Button type="button" size="xs" variant="outline" onClick={onOpenDetails}>
78 Details
79 </Button>
80 )}
81 </div>
82 )}
83 </div>
84 </div>
85​
86 {remote ? (
87 <RemoteRunBody run={run} />
88 ) : allAgents.length > 0 ? (
89 <div className="mt-1 space-y-px">
90 {run.phases.map((phase) => {
91 const rows = phase.agents.filter(
92 (agent) => visibleIndices === null || visibleIndices.has(agent.index),
93 );
94 if (rows.length === 0) {
95 return null;
96 }
97 return (
98 <div key={`${run.taskId}:${phase.index}`}>
99 <PhaseHeader phase={phase} />
100 {rows.map((agent) => (
101 <div key={`${run.taskId}:${agent.index}`} className="px-0.5 py-0.5">
102 <AgentRowContent agent={agent} />
103 </div>
104 ))}
105 </div>
106 );
107 })}
108 {hiddenCount > 0 && (
109 <button
110 type="button"
111 disabled={onOpenDetails === undefined}
112 onClick={onOpenDetails}
113 className={cn(
114 "w-full px-0.5 py-0.5 text-left text-[12px] text-muted-foreground/60 leading-5",
115 onOpenDetails &&
116 "cursor-pointer rounded-md hover:bg-accent/20 hover:text-muted-foreground",
117 )}
118 >
119 +{hiddenCount} more agents — open details
120 </button>
121 )}
122 </div>
123 ) : null}
124​
125 {run.handles?.warning !== undefined && (
126 <p className="mt-1.5 text-[11px] text-warning">{run.handles.warning}</p>
127 )}
128 </div>
129 );
131​
132function RemoteRunBody({ run }: { run: WorkflowRun }): ReactElement {
133 const sessionUrl = safeWorkflowSessionUrl(run.handles?.sessionUrl);
134 if (sessionUrl === undefined) {
135 return (
136 <p className="mt-1.5 px-0.5 text-[12px] text-muted-foreground/70 leading-5">
137 Running in the cloud
138 </p>
139 );
140 }
141 return (
142 <a
143 href={sessionUrl}
144 target="_blank"
145 rel="noopener noreferrer"
146 className="mt-1.5 flex items-center gap-1.5 rounded-md px-0.5 py-0.5 text-[12px] text-foreground/82 leading-5 hover:bg-accent/20 hover:text-foreground"
147 >
148 <ExternalLinkIcon className="size-3.5 shrink-0 text-muted-foreground" />
149 Running in the cloud — open session
150 </a>
151 );