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

thread-work-log.tsx

324 lines11.5 KB

1import * as Haptics from "expo-haptics";
2import { SymbolView, type SFSymbol } from "expo-symbols";
3import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
4import { useRouter } from "expo-router";
5import { LayoutAnimation, Pressable, useColorScheme, View } from "react-native";
6​
7import { AppText as Text } from "../../components/AppText";
8import { cn } from "../../lib/cn";
9import { buildThreadRoutePath } from "../../lib/routes";
10import type { ThreadFeedActivity } from "../../lib/threadActivity";
11import { useV2ItemSupport } from "../../state/v2-item-support";
12import { ThreadActivityInspector } from "./ThreadActivityInspector";
13​
14const MAX_VISIBLE_WORK_LOG_ENTRIES = 1;
15const WORK_LOG_LAYOUT_ANIMATION = {
16 duration: 180,
17 create: {
18 type: LayoutAnimation.Types.easeInEaseOut,
19 property: LayoutAnimation.Properties.opacity,
20 },
21 update: { type: LayoutAnimation.Types.easeInEaseOut },
22 delete: {
23 type: LayoutAnimation.Types.easeInEaseOut,
24 property: LayoutAnimation.Properties.opacity,
25 },
26} as const;
27​
28function triggerDisclosureFeedback() {
29 LayoutAnimation.configureNext(WORK_LOG_LAYOUT_ANIMATION);
30 void Haptics.selectionAsync();
32​
33function stripShellWrapper(value: string): string {
34 const trimmed = value.trim();
35 const match = trimmed.match(/^\/bin\/zsh -lc ['"]?([\s\S]*?)['"]?$/);
36 return (match?.[1] ?? trimmed).trim();
38​
39function compactActivityDetail(detail: string | null): string | null {
40 if (!detail) {
41 return null;
42 }
43​
44 const cleaned = stripShellWrapper(detail).replace(/\s+/g, " ").trim();
45 return cleaned.length > 0 ? cleaned : null;
47​
48function workRowSymbolName(icon: ThreadFeedActivity["icon"]): SFSymbol {
49 switch (icon) {
50 case "agent":
51 return "sparkles";
52 case "alert":
53 return "exclamationmark.triangle";
54 case "check":
55 return "checkmark";
56 case "command":
57 return "terminal";
58 case "edit":
59 return "square.and.pencil";
60 case "eye":
61 return "eye";
62 case "globe":
63 return "globe";
64 case "hammer":
65 return "hammer";
66 case "message":
67 return "bubble.left";
68 case "warning":
69 return "xmark";
70 case "wrench":
71 return "wrench";
72 case "zap":
73 return "bolt";
74 }
76​
77function ThreadActivityThreadLink(props: {
78 readonly activity: ThreadFeedActivity;
79 readonly environmentId: EnvironmentId;
80 readonly iconColor: import("react-native").ColorValue;
81}) {
82 const row = props.activity.projectedItem;
83 const support = useV2ItemSupport({
84 environmentId: props.environmentId,
85 sourceThreadId: row.sourceThreadId,
86 sourceItemId: row.sourceItemId,
87 });
88 const router = useRouter();
89 const item = row.item;
90 let targetThreadId: ThreadId | null = null;
91 let label = "Open related thread";
92​
93 if (item.type === "thread_created") {
94 targetThreadId = item.targetThreadId;
95 label = "Open created thread";
96 } else if (item.type === "subagent") {
97 targetThreadId = support.subagent?.childThreadId ?? item.childThreadId;
98 label = "Open subagent thread";
99 } else if (item.type === "fork") {
100 targetThreadId =
101 item.targetThreadId === row.sourceThreadId && item.source.type === "run"
102 ? item.source.threadId
103 : item.targetThreadId;
104 label = targetThreadId === item.targetThreadId ? "Open forked thread" : "Open parent thread";
105 }
106​
107 if (targetThreadId === null) return null;
108​
109 return (
110 <Pressable
111 accessibilityRole="link"
112 accessibilityLabel={label}
113 onPress={() => {
114 void Haptics.selectionAsync();
115 router.push(
116 buildThreadRoutePath({
117 environmentId: props.environmentId,
118 threadId: targetThreadId,
119 }),
120 );
121 }}
122 className="mx-2 mb-2 min-h-9 flex-row items-center justify-center gap-1.5 rounded-lg border border-neutral-300/50 px-2 dark:border-white/[0.08]"
123 >
124 <Text className="font-t3-medium text-2xs text-foreground">{label}</Text>
125 <SymbolView name="arrow.right" size={11} tintColor={props.iconColor} type="monochrome" />
126 </Pressable>
127 );
129​
130export function ThreadWorkLog(props: {
131 readonly activities: ReadonlyArray<ThreadFeedActivity>;
132 readonly copiedRowId: string | null;
133 readonly environmentId: EnvironmentId;
134 readonly expanded: boolean;
135 readonly expandedRows: Readonly<Record<string, boolean>>;
136 readonly iconSubtleColor: import("react-native").ColorValue;
137 readonly onCopyRow: (rowId: string, value: string) => void;
138 readonly onToggleGroup: () => void;
139 readonly onToggleRow: (rowId: string) => void;
140 readonly workspaceRoot?: string | null;
141}) {
142 const colorScheme = useColorScheme();
143 const pressedBackground = colorScheme === "dark" ? "rgba(255,255,255,0.05)" : "rgba(0,0,0,0.035)";
144 const rows = props.activities;
145​
146 if (rows.length === 0) {
147 return null;
148 }
149​
150 const hasOverflow = rows.length > MAX_VISIBLE_WORK_LOG_ENTRIES;
151 const visibleRows =
152 hasOverflow && !props.expanded ? rows.slice(-MAX_VISIBLE_WORK_LOG_ENTRIES) : rows;
153 const hiddenCount = rows.length - visibleRows.length;
154 const onlyToolRows = rows.every((row) => row.toolLike);
155​
156 return (
157 <View className="-mx-1 mb-3 px-1 py-0.5">
158 {!onlyToolRows ? (
159 <Text className="px-0.5 pb-0.5 font-t3-medium text-2xs text-foreground-muted opacity-60">
160 work log
161 </Text>
162 ) : null}
163​
164 <View className="gap-px">
165 {visibleRows.map((row) => {
166 const expanded = props.expandedRows[row.id] ?? false;
167 const canExpand = row.fullDetail !== null;
168 const detail = compactActivityDetail(row.detail);
169 const displayText = detail ? `${row.summary} ${detail}` : row.summary;
170 const iconIsDestructive = row.icon === "alert" || row.icon === "warning";
171​
172 return (
173 <View
174 key={row.id}
175 className={cn(
176 row.prominent &&
177 "mb-2 overflow-hidden rounded-xl border border-neutral-300/60 bg-card dark:border-white/[0.1]",
178 )}
179 >
180 <Pressable
181 accessibilityRole={canExpand ? "button" : undefined}
182 accessibilityLabel={displayText}
183 accessibilityHint={
184 canExpand
185 ? "Double tap to show full details. Long press to copy."
186 : "Long press to copy."
187 }
188 accessibilityState={canExpand ? { expanded } : undefined}
189 hitSlop={4}
190 onPress={() => {
191 if (canExpand) {
192 triggerDisclosureFeedback();
193 props.onToggleRow(row.id);
194 }
195 }}
196 onLongPress={() => props.onCopyRow(row.id, row.copyText)}
197 style={({ pressed }) => ({
198 backgroundColor: pressed ? pressedBackground : "transparent",
199 })}
200 className="rounded-md px-0.5 py-0.5"
201 >
202 <View className="min-h-9 flex-row items-center gap-1.5">
203 <View className="h-5 w-5 shrink-0 items-center justify-center">
204 <SymbolView
205 name={workRowSymbolName(row.icon)}
206 size={14}
207 weight="medium"
208 tintColor={iconIsDestructive ? "#e11d48" : props.iconSubtleColor}
209 type="monochrome"
210 />
211 </View>
212​
213 <Text
214 className="min-w-0 flex-1 text-xs leading-5 text-foreground"
215 numberOfLines={1}
216 >
217 <Text
218 className={cn(
219 "font-t3-medium text-foreground",
220 iconIsDestructive && "text-rose-600 dark:text-rose-400",
221 )}
222 >
223 {row.summary}
224 </Text>
225 {detail ? (
226 <Text className="text-foreground-muted opacity-60"> {detail}</Text>
227 ) : null}
228 </Text>
229​
230 <View className="shrink-0 flex-row items-center gap-px">
231 {props.copiedRowId === row.id ? (
232 <Text className="pr-1 font-t3-medium text-3xs text-emerald-600 dark:text-emerald-400">
233 Copied
234 </Text>
235 ) : null}
236 <View className="h-4 w-4 items-center justify-center">
237 {canExpand ? (
238 <SymbolView
239 name={expanded ? "chevron.up" : "chevron.down"}
240 size={11}
241 tintColor={props.iconSubtleColor}
242 type="monochrome"
243 />
244 ) : null}
245 </View>
246 <View className="h-4 w-4 items-center justify-center">
247 {row.status ? (
248 <SymbolView
249 name={
250 row.status === "failure"
251 ? "xmark"
252 : row.status === "success"
253 ? "checkmark"
254 : "minus"
255 }
256 size={11}
257 tintColor={row.status === "failure" ? "#e11d48" : props.iconSubtleColor}
258 type="monochrome"
259 />
260 ) : null}
261 </View>
262 </View>
263 </View>
264 </Pressable>
265​
266 {expanded && row.fullDetail ? (
267 <View className="ml-7 border-l border-neutral-300/60 pb-1.5 pl-3 pt-0.5 dark:border-white/[0.12]">
268 <ThreadActivityInspector
269 activity={row}
270 environmentId={props.environmentId}
271 iconColor={props.iconSubtleColor}
272 workspaceRoot={props.workspaceRoot}
273 />
274 </View>
275 ) : null}
276 {row.prominent ? (
277 <ThreadActivityThreadLink
278 activity={row}
279 environmentId={props.environmentId}
280 iconColor={props.iconSubtleColor}
281 />
282 ) : null}
283 </View>
284 );
285 })}
286 </View>
287​
288 {hasOverflow ? (
289 <Pressable
290 accessibilityRole="button"
291 accessibilityState={{ expanded: props.expanded }}
292 accessibilityLabel={
293 props.expanded
294 ? "Show fewer tool calls"
295 : `Show ${hiddenCount} previous tool ${hiddenCount === 1 ? "call" : "calls"}`
296 }
297 hitSlop={4}
298 onPress={() => {
299 triggerDisclosureFeedback();
300 props.onToggleGroup();
301 }}
302 style={({ pressed }) => ({
303 backgroundColor: pressed ? pressedBackground : "transparent",
304 })}
305 className="min-h-9 flex-row items-center gap-1.5 rounded-md px-0.5 py-0.5"
306 >
307 <View className="h-5 w-5 items-center justify-center">
308 <SymbolView
309 name={props.expanded ? "chevron.up" : "chevron.down"}
310 size={13}
311 tintColor={props.iconSubtleColor}
312 type="monochrome"
313 />
314 </View>
315 <Text className="font-t3-medium text-xs text-foreground opacity-80">
316 {props.expanded
317 ? "Show fewer tool calls"
318 : `+${hiddenCount} previous tool ${hiddenCount === 1 ? "call" : "calls"}`}
319 </Text>
320 </Pressable>
321 ) : null}
322 </View>
323 );