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

AgentActivity.tsx

398 lines13.2 KB

1import { HStack, Image, Spacer, Text, VStack, ZStack } from "@expo/ui/swift-ui";
2import type { ComponentProps } from "react";
3import {
4 accessibilityElement,
5 accessibilityLabel,
6 activityBackgroundTint,
7 font,
8 foregroundStyle,
9 frame,
10 layoutPriority,
11 lineLimit,
12 minimumScaleFactor,
13 padding,
14 resizable,
15 widgetURL,
16} from "@expo/ui/swift-ui/modifiers";
17import {
18 createLiveActivity,
19 type LiveActivityComponent,
20 type LiveActivityLayout,
21} from "expo-widgets";
22​
23type LiveActivityEnvironment = Parameters<LiveActivityComponent<AgentActivityProps>>[1];
24​
25export type AgentActivityPhase =
26 | "starting"
27 | "running"
28 | "waiting_for_approval"
29 | "waiting_for_input"
30 | "completed"
31 | "failed"
32 | "stale";
33​
34export interface AgentActivityRowProps {
35 readonly environmentId: string;
36 readonly threadId: string;
37 readonly projectTitle: string;
38 readonly threadTitle: string;
39 readonly modelTitle: string;
40 readonly phase: AgentActivityPhase;
41 readonly status: string;
42 readonly updatedAt: string;
43 readonly deepLink: string;
45​
46export interface AgentActivityProps {
47 readonly title: string;
48 readonly subtitle: string;
49 readonly activeCount: number;
50 readonly updatedAt: string;
51 readonly activities: ReadonlyArray<AgentActivityRowProps>;
53​
54// This function is serialized into the widget extension's JS bundle, so it
55// must stay self-contained: no references to module-scope helpers, only the
56// imported view/modifier factories.
57export function AgentActivity(
58 props: AgentActivityProps,
59 environment: LiveActivityEnvironment,
60): LiveActivityLayout {
61 "widget";
62​
63 // Hierarchical styles inherit the system's foreground treatment, including
64 // tinted and vibrant presentations, rather than resolving to a label color.
65 type Foreground = Parameters<typeof foregroundStyle>[0];
66 const primaryForeground = { type: "hierarchical", style: "primary" } as const;
67 const secondaryForeground = { type: "hierarchical", style: "secondary" } as const;
68 const monochrome =
69 environment.widgetRenderingMode === "accented" || environment.widgetRenderingMode === "vibrant";
70​
71 // On iPhone the LA sits on a dark material, but macOS (iPhone Mirroring /
72 // Mac notification center) renders it on a light one — so pick the web
73 // palette's light (-600) or dark (-300) variant off the color scheme.
74 const isLightScheme = environment.colorScheme === "light";
75 const phaseTint = (phase: AgentActivityPhase | undefined): Foreground => {
76 if (environment.isLuminanceReduced) {
77 return secondaryForeground;
78 }
79 if (monochrome) {
80 return primaryForeground;
81 }
82 switch (phase) {
83 case "waiting_for_approval":
84 return isLightScheme ? "#d97706" : "#fcd34d";
85 case "waiting_for_input":
86 return isLightScheme ? "#4f46e5" : "#a5b4fc";
87 case "failed":
88 return isLightScheme ? "#dc2626" : "#fca5a5";
89 case "completed":
90 return isLightScheme ? "#059669" : "#6ee7b7";
91 case "stale":
92 return secondaryForeground;
93 case "starting":
94 case "running":
95 default:
96 return isLightScheme ? "#0284c7" : "#7dd3fc";
97 }
98 };
99​
100 const phasePriority = (phase: AgentActivityPhase): number => {
101 if (phase === "waiting_for_approval" || phase === "waiting_for_input") return 0;
102 if (phase === "failed") return 1;
103 if (phase === "running" || phase === "starting") return 2;
104 return 3;
105 };
106 // Past the stale date the system stops vouching for the content, so every
107 // in-flight row degrades to "stale" rather than claiming an agent is still
108 // working.
109 const activities: ReadonlyArray<AgentActivityRowProps> = environment.isStale
110 ? props.activities.map((row) =>
111 row.phase === "completed" || row.phase === "failed"
112 ? row
113 : { ...row, phase: "stale", status: "Out of date" },
114 )
115 : props.activities;
116 const ordered = [...activities].sort((a, b) => phasePriority(a.phase) - phasePriority(b.phase));
117 const row0 = ordered[0];
118 const row1 = ordered[1];
119 const row2 = ordered[2];
120 const row3 = ordered[3];
121 const row4 = ordered[4];
122​
123 const attentionRows = activities.filter(
124 (row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input",
125 );
126 const attentionRow = attentionRows[0];
127 const failedRow = activities.find((row) => row.phase === "failed");
128 const heroRow = attentionRow ?? failedRow ?? row0;
129 const tint = phaseTint(heroRow?.phase);
130 const headerTint = attentionRow
131 ? phaseTint(attentionRow.phase)
132 : failedRow
133 ? phaseTint(failedRow.phase)
134 : tint;
135​
136 const allDone = props.activeCount === 0;
137 const doneLabel = failedRow ? "Failed" : "Done";
138 const outcomeLabel = failedRow ? "Agent work failed" : "Agent work completed";
139​
140 const agentWord = props.activeCount === 1 ? "agent" : "agents";
141 const agentsLabel = allDone
142 ? outcomeLabel
143 : environment.isStale
144 ? "Agent status out of date"
145 : `${props.activeCount} active ${agentWord}`;
146 const attentionSuffix =
147 attentionRows.length > 0
148 ? `${attentionRows.length} need${attentionRows.length === 1 ? "s" : ""} attention`
149 : "";
150 const activeLabel = allDone
151 ? doneLabel
152 : environment.isStale
153 ? "Out of date"
154 : `${props.activeCount} active`;
155 const showCompactGlyph = !attentionRow && !failedRow && !allDone && !environment.isStale;
156 const summary = attentionSuffix || activeLabel;
157​
158 // Any registered scheme variant routes back to this app; taps are delivered
159 // to the widget's containing app, so the prod scheme is safe for all builds.
160 const deepLinkRow = attentionRow ?? row0;
161 const deepLink =
162 deepLinkRow && deepLinkRow.deepLink.startsWith("/") && !deepLinkRow.deepLink.startsWith("//")
163 ? `t3code://${deepLinkRow.deepLink.slice(1)}`
164 : null;
165 const linkModifiers = deepLink ? [widgetURL(deepLink)] : [];
166​
167 type SFName = NonNullable<ComponentProps<typeof Image>["systemName"]>;
168 const phaseSymbol = (phase: AgentActivityPhase): SFName => {
169 switch (phase) {
170 case "waiting_for_approval":
171 return "exclamationmark.circle.fill";
172 case "waiting_for_input":
173 return "questionmark.circle.fill";
174 case "failed":
175 return "xmark.octagon.fill";
176 case "completed":
177 return "checkmark.circle.fill";
178 case "starting":
179 return "circle.dotted";
180 case "stale":
181 return "clock.arrow.circlepath";
182 case "running":
183 default:
184 // Matches the thread list's Working indicator.
185 return "circle.dashed";
186 }
187 };
188​
189 // SF Symbols, like the logo, ignore frame/foregroundStyle applied directly to
190 // the image; size + tint them through a container the resizable symbol fills.
191 const renderGlyph = (
192 systemName: SFName,
193 size: number,
194 color: Foreground,
195 modifiers: typeof linkModifiers = [],
196 ) => (
197 <HStack
198 modifiers={[frame({ width: size, height: size }), foregroundStyle(color), ...modifiers]}
199 >
200 <Image systemName={systemName} modifiers={[resizable()]} />
201 </HStack>
202 );
203​
204 const renderCompactRow = (row: AgentActivityRowProps) => (
205 <HStack spacing={7} alignment="center">
206 <Text
207 modifiers={[
208 font({ weight: "semibold", size: 13 }),
209 foregroundStyle(primaryForeground),
210 lineLimit(1),
211 ]}
212 >
213 {row.threadTitle}
214 </Text>
215 {/* No layoutPriority and no frame on the project: two bare texts take
216 their ideal width when it fits and shrink proportionally only when it
217 doesn't — so short rows never truncate, and long title + long project
218 truncate together. */}
219 <Text modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}>
220 {row.projectTitle}
221 </Text>
222 <Spacer minLength={8} />
223 <Text
224 modifiers={[
225 font({ weight: "semibold", size: 11 }),
226 foregroundStyle(phaseTint(row.phase)),
227 layoutPriority(1),
228 ]}
229 >
230 {row.status}
231 </Text>
232 </HStack>
233 );
234​
235 // Image views only honor `resizable` directly (frame/foregroundStyle are
236 // dropped), so we size it via a container frame the resizable image fills and
237 // tint it through the container's foreground style, which the template image
238 // inherits.
239 const renderLogo = (height: number, color: Foreground, modifiers: typeof linkModifiers = []) => (
240 <HStack
241 modifiers={[frame({ width: height * 1.5, height }), foregroundStyle(color), ...modifiers]}
242 >
243 <Image assetName="T3Mark" modifiers={[resizable()]} />
244 </HStack>
245 );
246​
247 return {
248 banner: (
249 <VStack
250 alignment="leading"
251 spacing={6}
252 modifiers={[
253 padding({ all: 14 }),
254 // A clear tint reveals iOS 26's glass material; older hosts keep the standard surface.
255 activityBackgroundTint(environment.isLiquidGlassAvailable ? "clear" : null),
256 ...(deepLink ? [widgetURL(deepLink)] : []),
257 ]}
258 >
259 <ZStack>
260 <HStack spacing={0} alignment="center">
261 {renderLogo(13, primaryForeground)}
262 <Spacer minLength={0} />
263 </HStack>
264 <HStack spacing={6} alignment="center">
265 <Spacer minLength={0} />
266 <Text
267 modifiers={[
268 font({ weight: "semibold", size: 13 }),
269 foregroundStyle(allDone ? headerTint : primaryForeground),
270 lineLimit(1),
271 ]}
272 >
273 {agentsLabel}
274 </Text>
275 {attentionSuffix ? (
276 <Text modifiers={[font({ size: 13 }), foregroundStyle(secondaryForeground)]}>·</Text>
277 ) : null}
278 {attentionSuffix ? (
279 <Text
280 modifiers={[
281 font({ weight: "semibold", size: 13 }),
282 foregroundStyle(headerTint),
283 lineLimit(1),
284 ]}
285 >
286 {attentionSuffix}
287 </Text>
288 ) : null}
289 <Spacer minLength={0} />
290 </HStack>
291 </ZStack>
292 {row0 ? renderCompactRow(row0) : null}
293 {row1 ? renderCompactRow(row1) : null}
294 {row2 ? renderCompactRow(row2) : null}
295 {row3 ? renderCompactRow(row3) : null}
296 {row4 ? renderCompactRow(row4) : null}
297 </VStack>
298 ),
299 bannerSmall: (
300 <VStack alignment="leading" spacing={5} modifiers={[padding({ all: 10 })]}>
301 <HStack spacing={7} alignment="center">
302 {renderLogo(14, primaryForeground)}
303 <Text
304 modifiers={[
305 font({ weight: "bold", size: 13 }),
306 foregroundStyle(headerTint),
307 lineLimit(1),
308 ]}
309 >
310 {attentionRows.length > 0 ? summary : activeLabel}
311 </Text>
312 <Spacer minLength={6} />
313 </HStack>
314 {row0 ? (
315 <HStack spacing={7} alignment="center">
316 <Text
317 modifiers={[
318 font({ weight: "semibold", size: 12 }),
319 foregroundStyle(primaryForeground),
320 lineLimit(1),
321 ]}
322 >
323 {row0.threadTitle}
324 </Text>
325 <Spacer minLength={6} />
326 <Text modifiers={[font({ size: 11 }), foregroundStyle(phaseTint(row0.phase))]}>
327 {row0.status}
328 </Text>
329 </HStack>
330 ) : null}
331 </VStack>
332 ),
333 compactLeading: renderLogo(14, tint, linkModifiers),
334 // While agents are only working, stay quiet: the thread list's Working
335 // glyph marks the activity without a count. Labels appear only for
336 // attention or outcomes.
337 compactTrailing: showCompactGlyph ? (
338 <HStack
339 modifiers={[
340 padding({ trailing: 2 }),
341 accessibilityElement("ignore"),
342 accessibilityLabel(agentsLabel),
343 ...linkModifiers,
344 ]}
345 >
346 {renderGlyph(phaseSymbol(heroRow?.phase ?? "running"), 14, tint)}
347 </HStack>
348 ) : (
349 <Text
350 modifiers={[
351 font({ weight: "semibold", size: 11 }),
352 foregroundStyle(tint),
353 lineLimit(1),
354 minimumScaleFactor(0.5),
355 ...linkModifiers,
356 ]}
357 >
358 {attentionRow
359 ? attentionRow.phase === "waiting_for_approval"
360 ? "Approval"
361 : "Input"
362 : failedRow
363 ? "Failed"
364 : activeLabel}
365 </Text>
366 ),
367 minimal: heroRow
368 ? renderGlyph(phaseSymbol(heroRow.phase), 13, phaseTint(heroRow.phase), linkModifiers)
369 : renderLogo(11, tint, linkModifiers),
370 expandedLeading: (
371 <HStack spacing={5} alignment="center" modifiers={[padding({ leading: 4, vertical: 4 })]}>
372 {renderLogo(15, tint)}
373 <Text modifiers={[font({ weight: "bold", size: 13 }), foregroundStyle(tint)]}>
374 {allDone ? doneLabel : `${props.activeCount}`}
375 </Text>
376 </HStack>
377 ),
378 expandedCenter: null,
379 expandedTrailing: null,
380 expandedBottom: (
381 <VStack
382 alignment="leading"
383 spacing={5}
384 modifiers={
385 deepLink
386 ? [padding({ vertical: 2, horizontal: 8 }), widgetURL(deepLink)]
387 : [padding({ vertical: 2, horizontal: 8 })]
388 }
389 >
390 {row0 ? renderCompactRow(row0) : null}
391 {row1 ? renderCompactRow(row1) : null}
392 {row2 ? renderCompactRow(row2) : null}
393 </VStack>
394 ),
395 };
397​
398export default createLiveActivity<AgentActivityProps>("AgentActivity", AgentActivity);