Open History to see the last commit
1import type { OrchestrationV2TurnItem, ThreadId } from "@t3tools/contracts";
2import type { TimestampFormat } from "@t3tools/contracts/settings";
3import {
4 BotIcon,
5 ExternalLinkIcon,
6 GitForkIcon,
7 MessageSquareIcon,
8 MinusIcon,
9 type LucideIcon,
10 XIcon,
11 ZapIcon,
12} from "lucide-react";
13
14import { formatShortTimestamp } from "../../timestampFormat";
15import { TimelineSystemDivider } from "./TimelineSystemDivider";
16
17const LIFECYCLE_TYPES = new Set<OrchestrationV2TurnItem["type"]>([
18 "run_interrupt_request",
19 "run_interrupt_result",
20 "compaction",
21 "handoff",
22 "fork",
23 "subagent",
24 "thread_created",
25]);
26
27export function isV2LifecycleItem(item: OrchestrationV2TurnItem): boolean {
28 return LIFECYCLE_TYPES.has(item.type);
29}
30
31export function V2LifecycleRow(props: {
32 readonly item: OrchestrationV2TurnItem;
33 readonly createdAt: string;
34 readonly timestampFormat: TimestampFormat;
35 readonly onOpenThread: (threadId: ThreadId) => void;
36}) {
37 const { item } = props;
38 if (item.type === "run_interrupt_request") {
39 return (
40 <div className="flex justify-end px-1 py-1" data-v2-item-type={item.type}>
41 <div className="flex max-w-[80%] items-center gap-2 text-xs text-destructive">
42 <span aria-hidden="true" className="font-mono">
43 ■
44 </span>
45 <span className="font-medium">Interrupt requested</span>
46 <span aria-hidden="true" className="opacity-50">
47 ·
48 </span>
49 <span className="font-medium">{item.message}</span>
50 <span className="text-[10px] text-muted-foreground">
51 {formatShortTimestamp(props.createdAt, props.timestampFormat)}
52 </span>
53 </div>
54 </div>
55 );
56 }
57 if (item.type === "run_interrupt_result") {
58 return (
59 <TimelineSystemDivider
60 label="Run interrupted"
61 detail={item.message}
62 tone="danger"
63 icon={XIcon}
64 />
65 );
66 }
67 if (item.type === "compaction") {
68 const tokenDetail =
69 item.beforeTokenCount === undefined && item.afterTokenCount === undefined
70 ? null
71 : `${item.beforeTokenCount ?? "?"} → ${item.afterTokenCount ?? "?"} tokens`;
72 return (
73 <TimelineSystemDivider
74 label="Context compacted"
75 detail={item.summary ?? tokenDetail}
76 icon={MinusIcon}
77 />
78 );
79 }
80 if (item.type === "handoff") {
81 return (
82 <TimelineSystemDivider
83 label="Context handoff"
84 icon={ZapIcon}
85 tone={item.status === "failed" ? "danger" : "neutral"}
86 detail={
87 item.summary ??
88 `${item.fromProviderInstanceIds.join(", ")} → ${item.toProviderInstanceId}`
89 }
90 />
91 );
92 }
93 if (item.type === "fork") {
94 const relatedThreadId = item.source.type === "run" ? item.source.threadId : item.targetThreadId;
95 return (
96 <TimelineSystemDivider
97 label={item.source.type === "run" ? "Forked from conversation" : "Conversation fork"}
98 icon={GitForkIcon}
99 actionLabel={item.source.type === "run" ? "Open source conversation" : "Open fork"}
100 onAction={() => props.onOpenThread(relatedThreadId)}
101 />
102 );
103 }
104 if (item.type === "thread_created") {
105 return (
106 <RelatedThreadCard
107 itemType={item.type}
108 icon={MessageSquareIcon}
109 title={item.title ?? "Created thread"}
110 detail={`${item.targetProviderInstanceId} · ${item.targetModel}`}
111 badge="created"
112 threadId={item.targetThreadId}
113 onOpenThread={props.onOpenThread}
114 />
115 );
116 }
117 if (item.type === "subagent") {
118 return (
119 <RelatedThreadCard
120 itemType={item.type}
121 icon={BotIcon}
122 title={subagentDisplayTitle(item.title ?? "Subagent")}
123 detail={item.result ?? item.progress ?? item.prompt}
124 badge={item.status}
125 threadId={item.childThreadId}
126 onOpenThread={props.onOpenThread}
127 />
128 );
129 }
130 return null;
131}
132
133function RelatedThreadCard(props: {
134 readonly itemType: "subagent" | "thread_created";
135 readonly icon: LucideIcon;
136 readonly title: string;
137 readonly detail: string;
138 readonly badge: string;
139 readonly threadId: ThreadId | null;
140 readonly onOpenThread: (threadId: ThreadId) => void;
141}) {
142 const Icon = props.icon;
143 const threadId = props.threadId;
144 const content = (
145 <>
146 <Icon className="size-3.5 shrink-0 text-muted-foreground" />
147 <span className="min-w-0 flex-1 truncate text-xs font-medium">{props.title}</span>
148 <span className="max-w-[50%] truncate text-xs text-muted-foreground">{props.detail}</span>
149 <span className="rounded-full border border-border/70 px-1.5 py-0.5 font-mono text-[10px] text-muted-foreground">
150 {props.badge}
151 </span>
152 {threadId === null ? null : (
153 <ExternalLinkIcon className="size-3 shrink-0 text-muted-foreground" aria-hidden="true" />
154 )}
155 </>
156 );
157
158 return threadId === null ? (
159 <div
160 data-v2-item-type={props.itemType}
161 className="flex min-w-0 items-center gap-2 rounded-lg border border-border/60 bg-card/30 px-3 py-2"
162 >
163 {content}
164 </div>
165 ) : (
166 <button
167 type="button"
168 data-v2-item-type={props.itemType}
169 aria-label={`Open ${props.title}`}
170 onClick={() => props.onOpenThread(threadId)}
171 className="flex w-full min-w-0 items-center gap-2 rounded-lg border border-border/60 bg-card/30 px-3 py-2 text-left transition-colors hover:bg-muted/50"
172 >
173 {content}
174 </button>
175 );
176}
177
178function subagentDisplayTitle(title: string): string {
179 return title.replace(/^Subagent:\s*/i, "");
180}