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