Open History to see the last commit
1import { WorkLogBlock } from "./WorkLog";
2import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { useAtomValue } from "@effect/atom-react";
4import { environmentThreadDetails } from "../../state/threads";
5import {
6 subagentGroupSummary,
7 summarizeSubagentStatuses,
8} from "@t3tools/client-runtime/state/subagent-display";
9import { useGT, useMessages, plural, part } from "gt-react";
10import { memo, use, useState } from "react";
11import { ChevronDownIcon } from "lucide-react";
12import { cn } from "~/lib/utils";
13import { Collapsible, CollapsibleTrigger, CollapsiblePanel } from "../ui/collapsible";
14import { EnvironmentSubagentAvatar, SubagentElapsed, V2LifecycleRow } from "./V2LifecycleRow";
15import { FeedbackRequestCard } from "./FeedbackRequestCard";
16import { HandoffRequestCard } from "./HandoffRequestCard";
17import { LinkRequestCard } from "./LinkRequestCard";
18import { SecretRequestCard } from "./SecretRequestCard";
19import { TimelineRowCtx, type TimelineRow } from "./timelineRowContext";
20import { subagentGroupTiming } from "./TimelineV2EventRow.logic";
21
22export function V2EventTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "event" }> }) {
23 const ctx = use(TimelineRowCtx);
24 const { item, visibility } = row.projectedItem;
25 const retryRunId =
26 visibility === "local" &&
27 item.type === "run_interrupt_result" &&
28 item.runId !== null &&
29 ctx.retryableWorkspacePreparationRunIds.has(item.runId)
30 ? item.runId
31 : null;
32 if (item.type === "subagent" && (row.subagents?.length ?? 1) > 1) {
33 return <V2SubagentGroup key={row.id} row={row} />;
34 }
35 if (item.type === "secret_request") {
36 return (
37 <SecretRequestCard
38 environmentId={ctx.activeThreadEnvironmentId}
39 item={item}
40 visibility={visibility}
41 />
42 );
43 }
44 if (item.type === "feedback_request") {
45 return (
46 <FeedbackRequestCard
47 environmentId={ctx.activeThreadEnvironmentId}
48 item={item}
49 visibility={visibility}
50 />
51 );
52 }
53 if (item.type === "handoff_request") {
54 return (
55 <HandoffRequestCard
56 environmentId={ctx.activeThreadEnvironmentId}
57 item={item}
58 visibility={visibility}
59 />
60 );
61 }
62 if (item.type === "link_request") {
63 return (
64 <LinkRequestCard
65 environmentId={ctx.activeThreadEnvironmentId}
66 item={item}
67 visibility={visibility}
68 />
69 );
70 }
71 return (
72 <V2LifecycleRow
73 environmentId={ctx.activeThreadEnvironmentId}
74 item={item}
75 resourceSummary={row.resourceSummary}
76 runs={ctx.runs}
77 onOpenThread={ctx.onOpenThread}
78 onRetryWorkspacePreparation={
79 retryRunId === null ? undefined : () => ctx.onRetryWorkspacePreparation?.(retryRunId)
80 }
81 />
82 );
83}
84
85const V2SubagentGroup = memo(function V2SubagentGroup({
86 row,
87}: {
88 row: Extract<TimelineRow, { kind: "event" }>;
89}) {
90 const ctx = use(TimelineRowCtx);
91 const gt = useGT();
92 const m = useMessages();
93 const groupId = `subagent-group:${row.id}`;
94 const [expanded, setExpanded] = useState(() =>
95 ctx.workGroupViewState.expandedEntries.has(groupId),
96 );
97 const members = (row.subagents ?? [row.projectedItem]).flatMap(({ item }) =>
98 item.type === "subagent" ? [item] : [],
99 );
100 const liveAgents = useAtomValue(
101 environmentThreadDetails.threadAtom(
102 scopeThreadRef(ctx.activeThreadEnvironmentId, row.projectedItem.item.threadId),
103 ),
104 (thread) => thread?.projection.subagents,
105 );
106 const agents = members.map((item) => {
107 const live = liveAgents?.find((agent) => agent.id === item.subagentId);
108 return {
109 item,
110 status: live?.status ?? item.status,
111 startedAt: live?.startedAt ?? item.startedAt,
112 completedAt: live?.completedAt ?? item.completedAt,
113 };
114 });
115 const summary = subagentGroupSummary(agents, m);
116 const label = gt`${plural(
117 { count: members.length },
118 {
119 one: part`${{ count: members.length }} subagent`,
120 other: part`${{ count: members.length }} subagents`,
121 },
122 )}`;
123 const statusSummary = summarizeSubagentStatuses(
124 agents.map(({ status }) => status),
125 m,
126 );
127 const toggleExpanded = (open: boolean) => {
128 ctx.onToggleWorkEntry(row.id);
129 if (open) ctx.workGroupViewState.expandedEntries.add(groupId);
130 else ctx.workGroupViewState.expandedEntries.delete(groupId);
131 setExpanded(open);
132 };
133 return (
134 <WorkLogBlock continues={row.continuesWorkLog}>
135 <Collapsible open={expanded} onOpenChange={toggleExpanded} data-subagent-group>
136 {/* pr-3.25 matches the panel border, padding, and row inset so the times line up. */}
137 <CollapsibleTrigger
138 aria-label={label}
139 aria-description={statusSummary}
140 className={cn(
141 "flex w-full min-w-0 items-center gap-2.5 py-2 pr-3.25 text-left transition-opacity hover:opacity-100",
142 expanded || summary.active
143 ? "text-foreground opacity-100"
144 : "text-muted-foreground opacity-55",
145 )}
146 >
147 <span className="flex shrink-0 items-center -space-x-1.5" aria-hidden>
148 {agents.slice(0, 3).map(({ item, status }) => (
149 <EnvironmentSubagentAvatar
150 key={item.id}
151 environmentId={ctx.activeThreadEnvironmentId}
152 providerInstanceId={item.providerInstanceId}
153 driver={item.driver}
154 remote={item.remoteChild}
155 status={agents.length === 1 ? status : undefined}
156 />
157 ))}
158 {agents.length > 3 ? (
159 <span className="relative inline-flex size-6 items-center justify-center rounded-full bg-muted text-3xs font-medium text-muted-foreground ring-2 ring-background">
160 +{agents.length - 3}
161 </span>
162 ) : null}
163 </span>
164 <span className="min-w-0 flex-1">
165 <span className="block text-xs font-semibold">{label}</span>
166 <span
167 className={cn(
168 "block truncate text-3xs text-muted-foreground",
169 summary.active ? "text-info" : summary.failed && "text-destructive",
170 )}
171 >
172 {statusSummary}
173 </span>
174 </span>
175 <span className="shrink-0 text-xs text-muted-foreground tabular-nums">
176 <SubagentElapsed agent={subagentGroupTiming(agents)} />
177 </span>
178 <ChevronDownIcon
179 aria-hidden
180 className={cn(
181 "size-3.5 shrink-0 text-muted-foreground transition-transform",
182 expanded && "rotate-180",
183 )}
184 />
185 </CollapsibleTrigger>
186 {/* Virtualized rows must settle before disclosure scroll anchoring resumes. */}
187 <CollapsiblePanel animate={false}>
188 {expanded ? (
189 <div className="mt-1 mb-1 rounded-lg border border-border/60 bg-card/30 p-1">
190 {members.map((item) => (
191 <V2LifecycleRow
192 environmentId={ctx.activeThreadEnvironmentId}
193 key={item.id}
194 item={item}
195 runs={ctx.runs}
196 onOpenThread={ctx.onOpenThread}
197 />
198 ))}
199 </div>
200 ) : null}
201 </CollapsiblePanel>
202 </Collapsible>
203 </WorkLogBlock>
204 );
205});