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