Open History to see the last commit
1import * as Haptics from "expo-haptics";
2import { SymbolView, type SFSymbol } from "expo-symbols";
3import { LayoutAnimation, Pressable, ScrollView, useColorScheme, View } from "react-native";
4
5import { AppText as Text } from "../../components/AppText";
6import { cn } from "../../lib/cn";
7import type { ThreadFeedActivity } from "../../lib/threadActivity";
8
9const MAX_VISIBLE_WORK_LOG_ENTRIES = 1;
10const WORK_LOG_LAYOUT_ANIMATION = {
11 duration: 180,
12 create: {
13 type: LayoutAnimation.Types.easeInEaseOut,
14 property: LayoutAnimation.Properties.opacity,
15 },
16 update: { type: LayoutAnimation.Types.easeInEaseOut },
17 delete: {
18 type: LayoutAnimation.Types.easeInEaseOut,
19 property: LayoutAnimation.Properties.opacity,
20 },
21} as const;
22
23function triggerDisclosureFeedback() {
24 LayoutAnimation.configureNext(WORK_LOG_LAYOUT_ANIMATION);
25 void Haptics.selectionAsync();
26}
27
28function stripShellWrapper(value: string): string {
29 const trimmed = value.trim();
30 const match = trimmed.match(/^\/bin\/zsh -lc ['"]?([\s\S]*?)['"]?$/);
31 return (match?.[1] ?? trimmed).trim();
32}
33
34function compactActivityDetail(detail: string | null): string | null {
35 if (!detail) {
36 return null;
37 }
38
39 const cleaned = stripShellWrapper(detail).replace(/\s+/g, " ").trim();
40 return cleaned.length > 0 ? cleaned : null;
41}
42
43function workRowSymbolName(icon: ThreadFeedActivity["icon"]): SFSymbol {
44 switch (icon) {
45 case "agent":
46 return "sparkles";
47 case "alert":
48 return "exclamationmark.triangle";
49 case "check":
50 return "checkmark";
51 case "command":
52 return "terminal";
53 case "edit":
54 return "square.and.pencil";
55 case "eye":
56 return "eye";
57 case "globe":
58 return "globe";
59 case "hammer":
60 return "hammer";
61 case "message":
62 return "bubble.left";
63 case "warning":
64 return "xmark";
65 case "wrench":
66 return "wrench";
67 case "zap":
68 return "bolt";
69 }
70}
71
72export function ThreadWorkLog(props: {
73 readonly activities: ReadonlyArray<ThreadFeedActivity>;
74 readonly copiedRowId: string | null;
75 readonly expanded: boolean;
76 readonly expandedRows: Readonly<Record<string, boolean>>;
77 readonly iconSubtleColor: import("react-native").ColorValue;
78 readonly onCopyRow: (rowId: string, value: string) => void;
79 readonly onToggleGroup: () => void;
80 readonly onToggleRow: (rowId: string) => void;
81}) {
82 const colorScheme = useColorScheme();
83 const pressedBackground = colorScheme === "dark" ? "rgba(255,255,255,0.05)" : "rgba(0,0,0,0.035)";
84 const rows = props.activities
85 .filter((activity) => !(activity.toolLike && activity.status === "neutral"))
86 .map((activity) => ({ ...activity, detail: compactActivityDetail(activity.detail) }));
87
88 if (rows.length === 0) {
89 return null;
90 }
91
92 const hasOverflow = rows.length > MAX_VISIBLE_WORK_LOG_ENTRIES;
93 const visibleRows =
94 hasOverflow && !props.expanded ? rows.slice(-MAX_VISIBLE_WORK_LOG_ENTRIES) : rows;
95 const hiddenCount = rows.length - visibleRows.length;
96 const onlyToolRows = rows.every((row) => row.toolLike);
97
98 return (
99 <View className="-mx-1 mb-3 px-1 py-0.5">
100 {!onlyToolRows ? (
101 <Text className="px-0.5 pb-0.5 font-t3-medium text-2xs text-foreground-muted opacity-60">
102 work log
103 </Text>
104 ) : null}
105
106 <View className="gap-px">
107 {visibleRows.map((row) => {
108 const expanded = props.expandedRows[row.id] ?? false;
109 const canExpand = row.fullDetail !== null;
110 const displayText = row.detail ? `${row.summary} ${row.detail}` : row.summary;
111 const iconIsDestructive = row.icon === "alert" || row.icon === "warning";
112
113 return (
114 <View key={row.id}>
115 <Pressable
116 accessibilityRole={canExpand ? "button" : undefined}
117 accessibilityLabel={displayText}
118 accessibilityHint={
119 canExpand
120 ? "Double tap to show full details. Long press to copy."
121 : "Long press to copy."
122 }
123 accessibilityState={canExpand ? { expanded } : undefined}
124 hitSlop={4}
125 onPress={() => {
126 if (canExpand) {
127 triggerDisclosureFeedback();
128 props.onToggleRow(row.id);
129 }
130 }}
131 onLongPress={() => props.onCopyRow(row.id, row.copyText)}
132 style={({ pressed }) => ({
133 backgroundColor: pressed ? pressedBackground : "transparent",
134 })}
135 className="rounded-md px-0.5 py-0.5"
136 >
137 <View className="min-h-9 flex-row items-center gap-1.5">
138 <View className="h-5 w-5 shrink-0 items-center justify-center">
139 <SymbolView
140 name={workRowSymbolName(row.icon)}
141 size={14}
142 weight="medium"
143 tintColor={iconIsDestructive ? "#e11d48" : props.iconSubtleColor}
144 type="monochrome"
145 />
146 </View>
147
148 <Text
149 className="min-w-0 flex-1 text-xs leading-5 text-foreground"
150 numberOfLines={1}
151 >
152 <Text
153 className={cn(
154 "font-t3-medium text-foreground",
155 iconIsDestructive && "text-rose-600 dark:text-rose-400",
156 )}
157 >
158 {row.summary}
159 </Text>
160 {row.detail ? (
161 <Text className="text-foreground-muted opacity-60"> {row.detail}</Text>
162 ) : null}
163 </Text>
164
165 <View className="shrink-0 flex-row items-center gap-px">
166 {props.copiedRowId === row.id ? (
167 <Text className="pr-1 font-t3-medium text-3xs text-emerald-600 dark:text-emerald-400">
168 Copied
169 </Text>
170 ) : null}
171 <View className="h-4 w-4 items-center justify-center">
172 {canExpand ? (
173 <SymbolView
174 name={expanded ? "chevron.up" : "chevron.down"}
175 size={11}
176 tintColor={props.iconSubtleColor}
177 type="monochrome"
178 />
179 ) : null}
180 </View>
181 <View className="h-4 w-4 items-center justify-center">
182 {row.status ? (
183 <SymbolView
184 name={
185 row.status === "failure"
186 ? "xmark"
187 : row.status === "success"
188 ? "checkmark"
189 : "minus"
190 }
191 size={11}
192 tintColor={row.status === "failure" ? "#e11d48" : props.iconSubtleColor}
193 type="monochrome"
194 />
195 ) : null}
196 </View>
197 </View>
198 </View>
199 </Pressable>
200
201 {expanded && row.fullDetail ? (
202 <View className="ml-7 border-l border-neutral-300/60 pb-1.5 pl-3 pt-0.5 dark:border-white/[0.12]">
203 <ScrollView
204 nestedScrollEnabled
205 directionalLockEnabled
206 showsVerticalScrollIndicator
207 style={{ maxHeight: 240 }}
208 contentContainerStyle={{ paddingRight: 8 }}
209 >
210 <Text
211 selectable
212 className="text-2xs leading-[17px] text-foreground-muted"
213 style={{ fontFamily: "ui-monospace" }}
214 >
215 {row.fullDetail}
216 </Text>
217 </ScrollView>
218 </View>
219 ) : null}
220 </View>
221 );
222 })}
223 </View>
224
225 {hasOverflow ? (
226 <Pressable
227 accessibilityRole="button"
228 accessibilityState={{ expanded: props.expanded }}
229 accessibilityLabel={
230 props.expanded
231 ? "Show fewer tool calls"
232 : `Show ${hiddenCount} previous tool ${hiddenCount === 1 ? "call" : "calls"}`
233 }
234 hitSlop={4}
235 onPress={() => {
236 triggerDisclosureFeedback();
237 props.onToggleGroup();
238 }}
239 style={({ pressed }) => ({
240 backgroundColor: pressed ? pressedBackground : "transparent",
241 })}
242 className="min-h-9 flex-row items-center gap-1.5 rounded-md px-0.5 py-0.5"
243 >
244 <View className="h-5 w-5 items-center justify-center">
245 <SymbolView
246 name={props.expanded ? "chevron.up" : "chevron.down"}
247 size={13}
248 tintColor={props.iconSubtleColor}
249 type="monochrome"
250 />
251 </View>
252 <Text className="font-t3-medium text-xs text-foreground opacity-80">
253 {props.expanded
254 ? "Show fewer tool calls"
255 : `+${hiddenCount} previous tool ${hiddenCount === 1 ? "call" : "calls"}`}
256 </Text>
257 </Pressable>
258 ) : null}
259 </View>
260 );
261}