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

TimelineV2EventRow.tsx

323 lines12.4 KB

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 source={ctx.turnItemSource}
130 />
131 </div>
132 </div>
133 </details>
134 );
135 }
136 return (
137 <section
138 className={cn(
139 "rounded-lg border px-3 py-2",
140 presentation.tone === "warning" && "border-warning/25 bg-warning/5",
141 presentation.tone === "danger" && "border-destructive/25 bg-destructive/5",
142 presentation.tone === "success" && "border-success/20 bg-success/5",
143 presentation.tone === "muted" && "border-border/60 bg-card/30",
144 )}
145 data-v2-item-type={item.type}
146 data-v2-item-visibility={visibility}
147 >
148 <div className="flex items-start gap-2.5">
149 <Icon
150 className={cn(
151 "mt-0.5 size-3.5 shrink-0",
152 presentation.tone === "warning" && "text-warning",
153 presentation.tone === "danger" && "text-destructive",
154 presentation.tone === "success" && "text-success",
155 presentation.tone === "muted" && "text-muted-foreground",
156 )}
157 />
158 <div className="min-w-0 flex-1">
159 <div className="flex flex-wrap items-center gap-x-2 gap-y-1">
160 <span className="text-xs font-medium text-foreground/90">{presentation.label}</span>
161 {item.status !== "completed" ? (
162 <span
163 className={cn(
164 "rounded-full border px-1.5 py-0.5 font-mono text-3xs",
165 item.status === "failed"
166 ? "border-destructive/40 text-destructive"
167 : "border-border/70 text-muted-foreground",
168 )}
169 >
170 {item.status}
171 </span>
172 ) : null}
173 {visibility !== "local" ? (
174 <span className="rounded-full bg-muted px-1.5 py-0.5 text-3xs text-muted-foreground">
175 {visibility === "inherited" ? "Inherited" : "Synthetic"}
176 </span>
177 ) : null}
178 </div>
179 {presentation.detail ? (
180 <div className="mt-1 text-xs leading-relaxed text-muted-foreground">
181 <ChatMarkdown
182 text={presentation.detail}
183 cwd={ctx.markdownCwd}
184 threadRef={ctx.threadRef ?? undefined}
185 skills={ctx.skills}
186 lineBreaks
187 />
188 </div>
189 ) : null}
190 {visibility === "inherited" ? (
191 <p className="mt-1 font-mono text-3xs text-muted-foreground/65">
192 From {sourceThreadId}
193 </p>
194 ) : null}
195 <div className="mt-2">
196 <V2ItemInspector
197 projectedItem={row.projectedItem}
198 environmentId={ctx.activeThreadEnvironmentId}
199 cwd={ctx.markdownCwd}
200 workspaceRoot={ctx.workspaceRoot}
201 onOpenThread={ctx.onOpenThread}
202 onOpenTurnDiff={ctx.onOpenTurnDiff}
203 onRollbackCheckpoint={ctx.onRollbackCheckpoint}
204 onImageExpand={ctx.onImageExpand}
205 source={ctx.turnItemSource}
206 />
207 </div>
208 </div>
209 </div>
210 </section>
211 );
213​
214const V2SubagentGroup = memo(function V2SubagentGroup({
215 row,
216}: {
217 row: Extract<TimelineRow, { kind: "event" }>;
218}) {
219 const ctx = use(TimelineRowCtx);
220 const groupId = `subagent-group:${row.id}`;
221 const [expanded, setExpanded] = useState(() =>
222 ctx.workGroupViewState.expandedEntries.has(groupId),
223 );
224 const members = (row.subagents ?? [row.projectedItem]).flatMap(({ item }) =>
225 item.type === "subagent" ? [item] : [],
226 );
227 const liveAgents = useAtomValue(
228 environmentThreadDetails.threadAtom(
229 scopeThreadRef(ctx.activeThreadEnvironmentId, row.projectedItem.item.threadId),
230 ),
231 (thread) => thread?.projection.subagents,
232 );
233 const agents = members.map((item) => {
234 const live = liveAgents?.find((agent) => agent.id === item.subagentId);
235 return {
236 item,
237 status: live?.status ?? item.status,
238 startedAt: live?.startedAt ?? item.startedAt,
239 completedAt: live?.completedAt ?? item.completedAt,
240 };
241 });
242 const summary = subagentGroupSummary(agents);
243 const label = `${members.length} ${members.length === 1 ? "subagent" : "subagents"}`;
244 const statusSummary = summarizeSubagentStatuses(agents.map(({ status }) => status));
245 const toggleExpanded = (open: boolean) => {
246 ctx.onToggleWorkEntry(row.id, expanded);
247 if (open) ctx.workGroupViewState.expandedEntries.add(groupId);
248 else ctx.workGroupViewState.expandedEntries.delete(groupId);
249 setExpanded(open);
250 };
251 return (
252 <WorkLogBlock continues={row.continuesWorkLog}>
253 <Collapsible open={expanded} onOpenChange={toggleExpanded} data-subagent-group>
254 <CollapsibleTrigger
255 aria-label={label}
256 aria-description={statusSummary}
257 className={cn(
258 "flex w-full min-w-0 items-center gap-3 py-2 text-left transition-opacity hover:opacity-100",
259 expanded || summary.active
260 ? "text-foreground opacity-100"
261 : "text-muted-foreground opacity-55",
262 )}
263 >
264 <span className="flex shrink-0 items-center -space-x-1.5" aria-hidden>
265 {agents.slice(0, 3).map(({ item, status }) => (
266 <EnvironmentSubagentAvatar
267 key={item.id}
268 environmentId={ctx.activeThreadEnvironmentId}
269 providerInstanceId={item.providerInstanceId}
270 driver={item.driver}
271 status={agents.length === 1 ? status : undefined}
272 />
273 ))}
274 {agents.length > 3 ? (
275 <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">
276 +{agents.length - 3}
277 </span>
278 ) : null}
279 </span>
280 <span className="min-w-0 flex-1">
281 <span className="block text-xs font-semibold">{label}</span>
282 <span
283 className={cn(
284 "block truncate text-3xs text-muted-foreground",
285 summary.active ? "text-info" : summary.failed && "text-destructive",
286 )}
287 >
288 {statusSummary}
289 </span>
290 </span>
291 <span className="shrink-0 text-xs text-muted-foreground tabular-nums">
292 <SubagentElapsed agent={subagentGroupTiming(agents)} />
293 </span>
294 <ChevronDownIcon
295 aria-hidden
296 className={cn(
297 "size-3.5 shrink-0 text-muted-foreground transition-transform",
298 expanded && "rotate-180",
299 )}
300 />
301 </CollapsibleTrigger>
302 {/* Virtualized rows must settle before disclosure scroll anchoring resumes. */}
303 <CollapsiblePanel animate={false}>
304 {expanded ? (
305 <div className="mt-1 mb-1 rounded-lg border border-border/60 bg-card/30 p-1">
306 {members.map((item) => (
307 <V2LifecycleRow
308 environmentId={ctx.activeThreadEnvironmentId}
309 key={item.id}
310 item={item}
311 createdAt={row.createdAt}
312 timestampFormat={ctx.timestampFormat}
313 runs={ctx.runs}
314 onOpenThread={ctx.onOpenThread}
315 />
316 ))}
317 </div>
318 ) : null}
319 </CollapsiblePanel>
320 </Collapsible>
321 </WorkLogBlock>
322 );
323});