Open History to see the last commit
1import { Circle, 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;
44}
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>;
52}
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 showCompactCount = !attentionRow && !allDone && !environment.isStale;
156 const compactCountLabel = props.activeCount > 99 ? "99+" : `${props.activeCount}`;
157 const summary = attentionSuffix || activeLabel;
158
159 // Any registered scheme variant routes back to this app; taps are delivered
160 // to the widget's containing app, so the prod scheme is safe for all builds.
161 const deepLinkRow = attentionRow ?? row0;
162 const deepLink =
163 deepLinkRow && deepLinkRow.deepLink.startsWith("/") && !deepLinkRow.deepLink.startsWith("//")
164 ? `t3code://${deepLinkRow.deepLink.slice(1)}`
165 : null;
166 const linkModifiers = deepLink ? [widgetURL(deepLink)] : [];
167
168 type SFName = NonNullable<ComponentProps<typeof Image>["systemName"]>;
169 const phaseSymbol = (phase: AgentActivityPhase): SFName => {
170 switch (phase) {
171 case "waiting_for_approval":
172 return "exclamationmark.circle.fill";
173 case "waiting_for_input":
174 return "questionmark.circle.fill";
175 case "failed":
176 return "xmark.octagon.fill";
177 case "completed":
178 return "checkmark.circle.fill";
179 case "starting":
180 return "circle.dotted";
181 case "stale":
182 return "clock.arrow.circlepath";
183 case "running":
184 default:
185 return "arrow.triangle.2.circlepath";
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 compactTrailing: showCompactCount ? (
335 <ZStack
336 modifiers={[
337 frame({ width: 28, height: 28 }),
338 accessibilityElement("ignore"),
339 accessibilityLabel(agentsLabel),
340 ...linkModifiers,
341 ]}
342 >
343 <Circle modifiers={[foregroundStyle(tint)]} />
344 <Text
345 modifiers={[
346 font({ weight: "bold", size: 14 }),
347 foregroundStyle("#000000"),
348 lineLimit(1),
349 minimumScaleFactor(0.75),
350 frame({ width: 24, height: 28 }),
351 ]}
352 >
353 {compactCountLabel}
354 </Text>
355 </ZStack>
356 ) : (
357 <Text
358 modifiers={[
359 font({ weight: "semibold", size: 11 }),
360 foregroundStyle(tint),
361 lineLimit(1),
362 minimumScaleFactor(0.5),
363 ...linkModifiers,
364 ]}
365 >
366 {attentionRow
367 ? attentionRow.phase === "waiting_for_approval"
368 ? "Approval"
369 : "Input"
370 : activeLabel}
371 </Text>
372 ),
373 minimal:
374 (attentionRow || failedRow || allDone) && heroRow
375 ? renderGlyph(phaseSymbol(heroRow.phase), 13, phaseTint(heroRow.phase), linkModifiers)
376 : renderLogo(11, tint, linkModifiers),
377 expandedLeading: (
378 <HStack spacing={5} alignment="center" modifiers={[padding({ leading: 4, vertical: 4 })]}>
379 {renderLogo(15, tint)}
380 <Text modifiers={[font({ weight: "bold", size: 13 }), foregroundStyle(tint)]}>
381 {allDone ? doneLabel : `${props.activeCount}`}
382 </Text>
383 </HStack>
384 ),
385 expandedCenter: null,
386 expandedTrailing: null,
387 expandedBottom: (
388 <VStack
389 alignment="leading"
390 spacing={5}
391 modifiers={
392 deepLink
393 ? [padding({ vertical: 2, horizontal: 8 }), widgetURL(deepLink)]
394 : [padding({ vertical: 2, horizontal: 8 })]
395 }
396 >
397 {row0 ? renderCompactRow(row0) : null}
398 {row1 ? renderCompactRow(row1) : null}
399 {row2 ? renderCompactRow(row2) : null}
400 </VStack>
401 ),
402 };
403}
404
405export default createLiveActivity<AgentActivityProps>("AgentActivity", AgentActivity);