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