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 ChatMarkdown from "../ChatMarkdown";
11import { ChevronDownIcon } from "lucide-react";
12import { cn } from "~/lib/utils";
13import { V2ItemInspector } from "./V2ItemInspector";
14import { Collapsible, CollapsibleTrigger, CollapsiblePanel } from "../ui/collapsible";
15import {
16 isV2LifecycleItem,
17 EnvironmentSubagentAvatar,
18 SubagentElapsed,
19 V2LifecycleRow,
20} from "./V2LifecycleRow";
21import { SecretRequestCard } from "./SecretRequestCard";
22import { TimelineRowCtx, type TimelineRow } from "./timelineRowContext";
23import { v2EventPresentation, subagentGroupTiming } from "./TimelineV2EventRow.logic";
24
25export function V2EventTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "event" }> }) {
26 const ctx = use(TimelineRowCtx);
27 const { item, visibility, sourceThreadId } = row.projectedItem;
28 if (item.type === "subagent" && (row.subagents?.length ?? 1) > 1) {
29 return <V2SubagentGroup key={row.id} row={row} />;
30 }
31 if (item.type === "secret_request") {
32 return (
33 <SecretRequestCard
34 environmentId={ctx.activeThreadEnvironmentId}
35 item={item}
36 visibility={visibility}
37 />
38 );
39 }
40 if (isV2LifecycleItem(item)) {
41 return (
42 <V2LifecycleRow
43 environmentId={ctx.activeThreadEnvironmentId}
44 item={item}
45 resourceSummary={row.resourceSummary}
46 createdAt={row.createdAt}
47 timestampFormat={ctx.timestampFormat}
48 runs={ctx.runs}
49 onOpenThread={ctx.onOpenThread}
50 />
51 );
52 }
53 const presentation = v2EventPresentation(item);
54 const Icon = presentation.icon;
55 if (item.type === "error") {
56 return (
57 <details
58 className={cn(
59 "group rounded-md border",
60 presentation.tone === "warning" && "border-warning/25 bg-warning/5",
61 presentation.tone === "danger" && "border-destructive/25 bg-destructive/5",
62 presentation.tone === "success" && "border-success/20 bg-success/5",
63 )}
64 data-v2-item-type={item.type}
65 data-v2-item-visibility={visibility}
66 data-v2-event-disclosure="true"
67 >
68 <summary className="flex min-w-0 cursor-pointer list-none items-center gap-2 px-2.5 py-1.5 text-xs [&::-webkit-details-marker]:hidden">
69 <Icon
70 className={cn(
71 "size-3.5 shrink-0",
72 presentation.tone === "warning" && "text-warning",
73 presentation.tone === "danger" && "text-destructive",
74 presentation.tone === "success" && "text-success",
75 )}
76 />
77 <span className="shrink-0 font-medium text-foreground/90">{presentation.label}</span>
78 {item.status !== "completed" ? (
79 <span
80 className={cn(
81 "shrink-0 rounded-full border px-1.5 py-0.5 font-mono text-3xs",
82 item.status === "failed"
83 ? "border-destructive/40 text-destructive"
84 : "border-border/70 text-muted-foreground",
85 )}
86 >
87 {item.status}
88 </span>
89 ) : null}
90 {presentation.detail ? (
91 <span className="min-w-0 flex-1 truncate text-muted-foreground/65">
92 {presentation.detail}
93 </span>
94 ) : null}
95 {visibility !== "local" ? (
96 <span className="shrink-0 rounded-full bg-muted px-1.5 py-0.5 text-3xs text-muted-foreground">
97 {visibility === "inherited" ? "Inherited" : "Synthetic"}
98 </span>
99 ) : null}
100 <ChevronDownIcon className="size-3 shrink-0 text-muted-foreground/60 transition-transform group-open:rotate-180" />
101 </summary>
102 <div className="border-t border-border/45 px-3 py-2 ps-8">
103 {presentation.detail ? (
104 <div className="text-xs leading-relaxed text-muted-foreground">
105 <ChatMarkdown
106 text={presentation.detail}
107 cwd={ctx.markdownCwd}
108 threadRef={ctx.threadRef ?? undefined}
109 skills={ctx.skills}
110 lineBreaks
111 />
112 </div>
113 ) : null}
114 {visibility === "inherited" ? (
115 <p className="mt-1 font-mono text-3xs text-muted-foreground/65">
116 From {sourceThreadId}
117 </p>
118 ) : null}
119 <div className={presentation.detail ? "mt-2" : undefined}>
120 <V2ItemInspector
121 projectedItem={row.projectedItem}
122 environmentId={ctx.activeThreadEnvironmentId}
123 cwd={ctx.markdownCwd}
124 workspaceRoot={ctx.workspaceRoot}
125 onOpenThread={ctx.onOpenThread}
126 onOpenTurnDiff={ctx.onOpenTurnDiff}
127 onRollbackCheckpoint={ctx.onRollbackCheckpoint}
128 onImageExpand={ctx.onImageExpand}
129 />
130 </div>
131 </div>
132 </details>
133 );
134 }
135 return (
136 <section
137 className={cn(
138 "rounded-lg border px-3 py-2",
139 presentation.tone === "warning" && "border-warning/25 bg-warning/5",
140 presentation.tone === "danger" && "border-destructive/25 bg-destructive/5",
141 presentation.tone === "success" && "border-success/20 bg-success/5",
142 presentation.tone === "muted" && "border-border/60 bg-card/30",
143 )}
144 data-v2-item-type={item.type}
145 data-v2-item-visibility={visibility}
146 >
147 <div className="flex items-start gap-2.5">
148 <Icon
149 className={cn(
150 "mt-0.5 size-3.5 shrink-0",
151 presentation.tone === "warning" && "text-warning",
152 presentation.tone === "danger" && "text-destructive",
153 presentation.tone === "success" && "text-success",
154 presentation.tone === "muted" && "text-muted-foreground",
155 )}
156 />
157 <div className="min-w-0 flex-1">
158 <div className="flex flex-wrap items-center gap-x-2 gap-y-1">
159 <span className="text-xs font-medium text-foreground/90">{presentation.label}</span>
160 {item.status !== "completed" ? (
161 <span
162 className={cn(
163 "rounded-full border px-1.5 py-0.5 font-mono text-3xs",
164 item.status === "failed"
165 ? "border-destructive/40 text-destructive"
166 : "border-border/70 text-muted-foreground",
167 )}
168 >
169 {item.status}
170 </span>
171 ) : null}
172 {visibility !== "local" ? (
173 <span className="rounded-full bg-muted px-1.5 py-0.5 text-3xs text-muted-foreground">
174 {visibility === "inherited" ? "Inherited" : "Synthetic"}
175 </span>
176 ) : null}
177 </div>
178 {presentation.detail ? (
179 <div className="mt-1 text-xs leading-relaxed text-muted-foreground">
180 <ChatMarkdown
181 text={presentation.detail}
182 cwd={ctx.markdownCwd}
183 threadRef={ctx.threadRef ?? undefined}
184 skills={ctx.skills}
185 lineBreaks
186 />
187 </div>
188 ) : null}
189 {visibility === "inherited" ? (
190 <p className="mt-1 font-mono text-3xs text-muted-foreground/65">
191 From {sourceThreadId}
192 </p>
193 ) : null}
194 <div className="mt-2">
195 <V2ItemInspector
196 projectedItem={row.projectedItem}
197 environmentId={ctx.activeThreadEnvironmentId}
198 cwd={ctx.markdownCwd}
199 workspaceRoot={ctx.workspaceRoot}
200 onOpenThread={ctx.onOpenThread}
201 onOpenTurnDiff={ctx.onOpenTurnDiff}
202 onRollbackCheckpoint={ctx.onRollbackCheckpoint}
203 onImageExpand={ctx.onImageExpand}
204 />
205 </div>
206 </div>
207 </div>
208 </section>
209 );
210}
211
212const V2SubagentGroup = memo(function V2SubagentGroup({
213 row,
214}: {
215 row: Extract<TimelineRow, { kind: "event" }>;
216}) {
217 const ctx = use(TimelineRowCtx);
218 const groupId = `subagent-group:${row.id}`;
219 const [expanded, setExpanded] = useState(() =>
220 ctx.workGroupViewState.expandedEntries.has(groupId),
221 );
222 const members = (row.subagents ?? [row.projectedItem]).flatMap(({ item }) =>
223 item.type === "subagent" ? [item] : [],
224 );
225 const liveAgents = useAtomValue(
226 environmentThreadDetails.threadAtom(
227 scopeThreadRef(ctx.activeThreadEnvironmentId, row.projectedItem.item.threadId),
228 ),
229 (thread) => thread?.projection.subagents,
230 );
231 const agents = members.map((item) => {
232 const live = liveAgents?.find((agent) => agent.id === item.subagentId);
233 return {
234 item,
235 status: live?.status ?? item.status,
236 startedAt: live?.startedAt ?? item.startedAt,
237 completedAt: live?.completedAt ?? item.completedAt,
238 };
239 });
240 const summary = subagentGroupSummary(agents);
241 const label = `${members.length} ${members.length === 1 ? "subagent" : "subagents"}`;
242 const statusSummary = summarizeSubagentStatuses(agents.map(({ status }) => status));
243 const toggleExpanded = (open: boolean) => {
244 ctx.onToggleWorkEntry(row.id, expanded);
245 if (open) ctx.workGroupViewState.expandedEntries.add(groupId);
246 else ctx.workGroupViewState.expandedEntries.delete(groupId);
247 setExpanded(open);
248 };
249 return (
250 <WorkLogBlock continues={row.continuesWorkLog}>
251 <Collapsible open={expanded} onOpenChange={toggleExpanded} data-subagent-group>
252 <CollapsibleTrigger
253 aria-label={label}
254 aria-description={statusSummary}
255 className={cn(
256 "flex w-full min-w-0 items-center gap-3 py-2 text-left transition-opacity hover:opacity-100",
257 expanded || summary.active
258 ? "text-foreground opacity-100"
259 : "text-muted-foreground opacity-55",
260 )}
261 >
262 <span className="flex shrink-0 items-center -space-x-1.5" aria-hidden>
263 {agents.slice(0, 3).map(({ item, status }) => (
264 <EnvironmentSubagentAvatar
265 key={item.id}
266 environmentId={ctx.activeThreadEnvironmentId}
267 providerInstanceId={item.providerInstanceId}
268 driver={item.driver}
269 status={agents.length === 1 ? status : undefined}
270 />
271 ))}
272 {agents.length > 3 ? (
273 <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">
274 +{agents.length - 3}
275 </span>
276 ) : null}
277 </span>
278 <span className="min-w-0 flex-1">
279 <span className="block text-xs font-semibold">{label}</span>
280 <span
281 className={cn(
282 "block truncate text-3xs text-muted-foreground",
283 summary.active ? "text-info" : summary.failed && "text-destructive",
284 )}
285 >
286 {statusSummary}
287 </span>
288 </span>
289 <span className="shrink-0 text-xs text-muted-foreground tabular-nums">
290 <SubagentElapsed agent={subagentGroupTiming(agents)} />
291 </span>
292 <ChevronDownIcon
293 aria-hidden
294 className={cn(
295 "size-3.5 shrink-0 text-muted-foreground transition-transform",
296 expanded && "rotate-180",
297 )}
298 />
299 </CollapsibleTrigger>
300 {/* Virtualized rows must settle before disclosure scroll anchoring resumes. */}
301 <CollapsiblePanel animate={false}>
302 {expanded ? (
303 <div className="mt-1 mb-1 rounded-lg border border-border/60 bg-card/30 p-1">
304 {members.map((item) => (
305 <V2LifecycleRow
306 environmentId={ctx.activeThreadEnvironmentId}
307 key={item.id}
308 item={item}
309 createdAt={row.createdAt}
310 timestampFormat={ctx.timestampFormat}
311 runs={ctx.runs}
312 onOpenThread={ctx.onOpenThread}
313 />
314 ))}
315 </div>
316 ) : null}
317 </CollapsiblePanel>
318 </Collapsible>
319 </WorkLogBlock>
320 );
321});