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

V2LifecycleRow.tsx

351 lines12.0 KB

1import { useThreadShell, useProject } from "../../state/entities";
2import { SubagentTooltipContent } from "./SubagentTooltipContent";
3import { useAtomValue } from "@effect/atom-react";
4import { scopeThreadRef, scopeProjectRef } from "@t3tools/client-runtime/environment";
5import { environmentThreadDetails } from "../../state/threads";
6import { ThreadRelationshipIcon } from "./ThreadRelationshipIcon";
7import { WorkLogButton, WorkLogRow } from "./WorkLog";
8import { resolveHandoffEndpoints, type HandoffTimelineRun } from "@t3tools/client-runtime/handoff";
9import { Fragment } from "react";
10import { formatSubagentDisplayTitle } from "@t3tools/client-runtime/state/subagent-display";
11import {
12 ProviderDriverKind,
13 type OrchestrationV2TurnItem,
14 type ProviderInstanceId,
15 type ServerProvider,
16 type ThreadId,
17 type EnvironmentId,
18 type NodeId,
19 type ScopedThreadRef,
20} from "@t3tools/contracts";
21import type { TimestampFormat } from "@t3tools/contracts/settings";
22import {
23 ArrowRightLeftIcon,
24 ArrowRightIcon,
25 GitForkIcon,
26 MessageSquareIcon,
27 MinusIcon,
28 XIcon,
29} from "lucide-react";
30​
31import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip";
32import { getProviderInstanceEntry } from "../../providerInstances";
33import { formatShortTimestamp } from "../../timestampFormat";
34import { getTriggerDisplayModelName } from "./providerIconUtils";
35import { ProviderInstanceIcon } from "./ProviderInstanceIcon";
36import { TimelineSystemDivider } from "./TimelineSystemDivider";
37import { Button, InlineButton } from "../ui/button";
38import { T3Wordmark } from "../T3Wordmark";
39​
40const LIFECYCLE_TYPES = new Set<OrchestrationV2TurnItem["type"]>([
41 "run_interrupt_request",
42 "run_interrupt_result",
43 "compaction",
44 "handoff",
45 "fork",
46 "subagent",
47 "thread_created",
48]);
49​
50export function isV2LifecycleItem(item: OrchestrationV2TurnItem): boolean {
51 return LIFECYCLE_TYPES.has(item.type);
53​
54export type { HandoffTimelineRun } from "@t3tools/client-runtime/handoff";
55​
56export function V2LifecycleRow(props: {
57 readonly item: OrchestrationV2TurnItem;
58 readonly environmentId: EnvironmentId;
59 readonly resourceSummary?: boolean | undefined;
60 readonly createdAt: string;
61 readonly timestampFormat: TimestampFormat;
62 readonly providerStatuses: ReadonlyArray<ServerProvider>;
63 readonly runs: ReadonlyArray<HandoffTimelineRun>;
64 readonly onOpenThread: (threadId: ThreadId) => void;
65}) {
66 const { item } = props;
67 if (item.type === "run_interrupt_request") {
68 return (
69 <div className="flex justify-end px-1 py-1" data-v2-item-type={item.type}>
70 <div className="flex max-w-[80%] items-center gap-2 text-xs text-destructive">
71 <span aria-hidden="true" className="font-mono">
72 ■
73 </span>
74 <span className="font-medium">Interrupt requested</span>
75 <span aria-hidden="true" className="opacity-50">
76 ·
77 </span>
78 <span className="font-medium">{item.message}</span>
79 <span className="text-[10px] text-muted-foreground">
80 {formatShortTimestamp(props.createdAt, props.timestampFormat)}
81 </span>
82 </div>
83 </div>
84 );
85 }
86 if (item.type === "run_interrupt_result") {
87 return (
88 <TimelineSystemDivider
89 label="Run interrupted"
90 detail={item.message}
91 tone="danger"
92 icon={XIcon}
93 />
94 );
95 }
96 if (item.type === "compaction") {
97 const tokenDetail =
98 item.beforeTokenCount === undefined && item.afterTokenCount === undefined
99 ? null
100 : `${item.beforeTokenCount ?? "?"} → ${item.afterTokenCount ?? "?"} tokens`;
101 const label =
102 item.status === "failed"
103 ? "Context compaction failed"
104 : item.status === "cancelled" || item.status === "interrupted"
105 ? "Context compaction stopped"
106 : item.status === "pending" || item.status === "running" || item.status === "waiting"
107 ? "Compacting context"
108 : "Context compacted";
109 return (
110 <TimelineSystemDivider label={label} detail={item.summary ?? tokenDetail} icon={MinusIcon} />
111 );
112 }
113 if (item.type === "handoff") {
114 const { from: fromEndpoints, to } = resolveHandoffEndpoints(item, props.runs);
115 return (
116 <TimelineSystemDivider
117 label="Context handoff"
118 icon={ArrowRightLeftIcon}
119 showDetailSeparator={false}
120 tone={item.status === "failed" ? "danger" : "neutral"}
121 detail={
122 <span className="inline-flex min-w-0 flex-wrap items-center justify-center gap-1.5">
123 {fromEndpoints.map((endpoint, index) => (
124 <Fragment key={`${endpoint.instanceId}:${endpoint.model ?? ""}`}>
125 {index > 0 ? (
126 <span aria-hidden="true" className="-ml-1">
127 ,
128 </span>
129 ) : null}
130 <HandoffEndpoint
131 providers={props.providerStatuses}
132 instanceId={endpoint.instanceId}
133 model={endpoint.model}
134 />
135 </Fragment>
136 ))}
137 {fromEndpoints.length > 0 ? (
138 <ArrowRightIcon aria-hidden="true" className="size-3 shrink-0" />
139 ) : null}
140 <HandoffEndpoint
141 providers={props.providerStatuses}
142 instanceId={item.toProviderInstanceId}
143 model={to.model}
144 />
145 </span>
146 }
147 />
148 );
149 }
150 if (item.type === "fork") {
151 const relatedThreadId = item.source.type === "run" ? item.source.threadId : item.targetThreadId;
152 return (
153 <TimelineSystemDivider
154 label={item.source.type === "run" ? "Forked from conversation" : "Conversation fork"}
155 icon={GitForkIcon}
156 actionLabel={item.source.type === "run" ? "Open source conversation" : "Open fork"}
157 onAction={() => props.onOpenThread(relatedThreadId)}
158 />
159 );
160 }
161 if (item.type === "thread_created") {
162 if (props.resourceSummary) {
163 return (
164 <div
165 data-v2-item-type={item.type}
166 className="flex min-w-0 items-center gap-3 rounded-lg border border-border/60 p-3"
167 >
168 <span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-muted/60">
169 <MessageSquareIcon className="size-4 text-secondary-label" aria-hidden />
170 </span>
171 <span className="min-w-0 flex-1 truncate text-sm font-medium">
172 {item.title ?? "Created thread"}
173 </span>
174 <Button
175 size="xs"
176 variant="outline"
177 aria-label={`Open ${item.title ?? "created thread"}`}
178 onClick={() => props.onOpenThread(item.targetThreadId)}
179 >
180 Open chat
181 </Button>
182 </div>
183 );
184 }
185 return (
186 <WorkLogRow
187 data-v2-item-type={item.type}
188 icon={<T3Wordmark className="size-4 text-icon-muted" aria-hidden />}
189 label={<>Created thread{item.title ? ` · ${item.title}` : ""}</>}
190 trailing={
191 <InlineButton
192 variant="link"
193 aria-label={`Open ${item.title ?? "created thread"}`}
194 onClick={() => props.onOpenThread(item.targetThreadId)}
195 >
196 Open chat
197 </InlineButton>
198 }
199 />
200 );
201 }
202 if (item.type === "subagent") {
203 return (
204 <SubagentTimelineLink
205 parentRef={scopeThreadRef(props.environmentId, item.threadId)}
206 subagentId={item.subagentId}
207 status={item.status}
208 driver={item.driver}
209 provider={props.providerStatuses.find(
210 (provider) => provider.instanceId === item.providerInstanceId,
211 )}
212 title={formatSubagentDisplayTitle(item.title ?? "Subagent")}
213 result={item.result}
214 progress={item.progress}
215 threadId={item.childThreadId}
216 onOpenThread={props.onOpenThread}
217 />
218 );
219 }
220 return null;
222​
223function SubagentTimelineLink(props: {
224 readonly parentRef: ScopedThreadRef;
225 readonly subagentId: NodeId;
226 readonly driver: ProviderDriverKind;
227 readonly provider: ServerProvider | undefined;
228 readonly title: string;
229 readonly result: string | null;
230 readonly progress: string | undefined;
231 readonly status: OrchestrationV2TurnItem["status"];
232 readonly threadId: ThreadId | null;
233 readonly onOpenThread: (threadId: ThreadId) => void;
234}) {
235 const agent = useAtomValue(
236 environmentThreadDetails.threadAtom(props.parentRef),
237 (thread) => thread?.projection.subagents.find((agent) => agent.id === props.subagentId) ?? null,
238 );
239 const threadId = props.threadId;
240 const statusLabel = props.status.replaceAll("_", " ");
241 const icon = (
242 <ThreadRelationshipIcon driver={props.driver} provider={props.provider} status={props.status} />
243 );
244 const label = <span className="block truncate">{props.title}</span>;
245 return (
246 <Tooltip>
247 <TooltipTrigger
248 delay={200}
249 render={
250 threadId === null ? (
251 <WorkLogRow
252 data-v2-item-type="subagent"
253 aria-description={statusLabel}
254 icon={icon}
255 label={label}
256 />
257 ) : (
258 <WorkLogButton
259 data-v2-item-type="subagent"
260 aria-label={`Open ${props.title}`}
261 aria-description={statusLabel}
262 onClick={() => props.onOpenThread(threadId)}
263 icon={icon}
264 label={label}
265 />
266 )
267 }
268 />
269 <TooltipPopup>
270 <SubagentTimelineTooltip
271 {...props}
272 model={agent?.model ?? null}
273 status={agent?.status ?? props.status}
274 result={agent?.result ?? props.result}
275 progress={agent?.progress ?? props.progress}
276 />
277 </TooltipPopup>
278 </Tooltip>
279 );
281​
282function SubagentTimelineTooltip(
283 props: Parameters<typeof SubagentTimelineLink>[0] & { model: string | null },
284) {
285 const environmentId = props.parentRef.environmentId;
286 const parent = useThreadShell(props.parentRef)?.source;
287 const child = useThreadShell(
288 props.threadId ? scopeThreadRef(environmentId, props.threadId) : null,
289 )?.source;
290 const parentProject = useProject(
291 parent ? scopeProjectRef(environmentId, parent.projectId) : null,
292 );
293 const childProject = useProject(child ? scopeProjectRef(environmentId, child.projectId) : null);
294 return (
295 <SubagentTooltipContent
296 title={formatSubagentDisplayTitle(child?.title ?? props.title)}
297 model={props.model}
298 provider={props.provider}
299 status={props.status}
300 result={props.result}
301 progress={props.progress}
302 parentThread={parent}
303 childThread={child}
304 parentProject={parentProject ?? undefined}
305 childProject={childProject ?? undefined}
306 />
307 );
309​
310function HandoffEndpoint(props: {
311 readonly providers: ReadonlyArray<ServerProvider>;
312 readonly instanceId: ProviderInstanceId;
313 readonly model?: string | undefined;
314}) {
315 const entry = getProviderInstanceEntry(props.providers, props.instanceId);
316 const model = props.model?.trim();
317 const providerModel =
318 model === undefined || model.length === 0
319 ? undefined
320 : entry?.models.find((candidate) => candidate.slug === model);
321 const label =
322 providerModel !== undefined
323 ? getTriggerDisplayModelName(providerModel)
324 : model !== undefined && model.length > 0
325 ? model
326 : (entry?.displayName ?? props.instanceId);
327 return (
328 <Tooltip>
329 <TooltipTrigger
330 render={
331 <span
332 tabIndex={0}
333 className="inline-flex min-w-0 items-center gap-1 rounded-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
334 >
335 <ProviderInstanceIcon
336 driverKind={entry?.driverKind ?? ProviderDriverKind.make(props.instanceId)}
337 displayName={entry?.displayName ?? props.instanceId}
338 acpRegistryAgentId={entry?.acpRegistryAgentId}
339 acpRegistryIconUrl={entry?.acpRegistryIconUrl}
340 iconClassName="size-3"
341 />
342 <span className="truncate">{label}</span>
343 </span>
344 }
345 />
346 <TooltipPopup>
347 {entry?.displayName ?? props.instanceId} · {label}
348 </TooltipPopup>
349 </Tooltip>
350 );