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 // Status tints mirror the web sidebar's pills
69 // (apps/web/src/components/Sidebar.logic.ts resolveThreadStatusPill): amber
70 // for approval, indigo for input, sky for working, emerald for completed.
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"; // amber-600 / amber-300
85 case "waiting_for_input":
86 return isLightScheme ? "#4f46e5" : "#a5b4fc"; // indigo-600 / indigo-300
87 case "failed":
88 return isLightScheme ? "#dc2626" : "#fca5a5"; // red-600 / red-300
89 case "completed":
90 return isLightScheme ? "#059669" : "#6ee7b7"; // emerald-600 / emerald-300
91 case "starting":
92 case "running":
93 default:
94 return isLightScheme ? "#0284c7" : "#7dd3fc"; // sky-600 / sky-300
95 }
96 };
97
98 // Order attention-first so whatever needs the user floats to the top of every
99 // presentation, then failures, then in-flight work, then finished/stale.
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 const ordered = [...props.activities].sort(
107 (a, b) => phasePriority(a.phase) - phasePriority(b.phase),
108 );
109 const row0 = ordered[0];
110 const row1 = ordered[1];
111 const row2 = ordered[2];
112 const row3 = ordered[3];
113 const row4 = ordered[4];
114
115 const attentionRows = props.activities.filter(
116 (row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input",
117 );
118 const attentionRow = attentionRows[0];
119 const failedRow = props.activities.find((row) => row.phase === "failed");
120 const heroRow = attentionRow ?? failedRow ?? row0;
121 const tint = phaseTint(heroRow?.phase);
122 // Headline count leans on the accent when a human is actually blocked.
123 const headerTint = attentionRow
124 ? phaseTint(attentionRow.phase)
125 : failedRow
126 ? phaseTint(failedRow.phase)
127 : tint;
128
129 // With nothing active the aggregate only carries recently finished work, so
130 // "0 active agents" (and a lone "0" in the expanded island) read as broken.
131 // Lead with the outcome instead. The outcome is derived here from the rows
132 // rather than taken from the server subtitle (which keys off the newest
133 // terminal row): every presentation — header text, tint, count slots,
134 // minimal glyph — must agree, and a failure anywhere should dominate a
135 // newer success.
136 const allDone = props.activeCount === 0;
137 const doneLabel = failedRow ? "Failed" : "Done";
138 const outcomeLabel = failedRow ? "Agent work failed" : "Agent work completed";
139
140 // Header copy: "5 active agents" + (", 1 needs attention"). The banner renders
141 // the two parts in-line so the attention half can carry the accent color;
142 // `summary` is the short form for tight spots (expanded center, watch card).
143 const agentWord = props.activeCount === 1 ? "agent" : "agents";
144 const agentsLabel = allDone ? outcomeLabel : `${props.activeCount} active ${agentWord}`;
145 const attentionSuffix =
146 attentionRows.length > 0
147 ? `${attentionRows.length} need${attentionRows.length === 1 ? "s" : ""} attention`
148 : "";
149 const activeLabel = allDone ? doneLabel : `${props.activeCount} active`;
150 const summary = attentionSuffix || activeLabel;
151
152 // Any registered scheme variant routes back to this app; taps are delivered
153 // to the widget's containing app, so the prod scheme is safe for all builds.
154 const deepLinkRow = attentionRow ?? row0;
155 const deepLink =
156 deepLinkRow && deepLinkRow.deepLink.startsWith("/") && !deepLinkRow.deepLink.startsWith("//")
157 ? `t3code://${deepLinkRow.deepLink.slice(1)}`
158 : null;
159
160 // A scannable status glyph per phase — reads faster than colored words and
161 // ties the compact / expanded / banner / watch presentations together.
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 // Single-line row used by every presentation: glyph, title, inline project,
192 // status. The project and status carry layoutPriority(1) so when space runs
193 // out it's the title that truncates, never the (short) project name or the
194 // status label. Single-line keeps rows inside the expanded island's hard
195 // height budget (~160pt) and lets the banner fit more agents.
196 const renderCompactRow = (row: AgentActivityRowProps) => (
197 <HStack spacing={7} alignment="center">
198 <Text
199 modifiers={[
200 font({ weight: "semibold", size: 13 }),
201 foregroundStyle(primaryForeground),
202 lineLimit(1),
203 ]}
204 >
205 {row.threadTitle}
206 </Text>
207 {/* No layoutPriority and no frame on the project: two bare texts take
208 their ideal width when it fits and shrink proportionally only when it
209 doesn't — so short rows never truncate, and long title + long project
210 truncate together. (A maxWidth frame is greedy and reserved its full
211 width even for short names; layoutPriority let the project starve the
212 title.) */}
213 <Text modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}>
214 {row.projectTitle}
215 </Text>
216 <Spacer minLength={8} />
217 <Text
218 modifiers={[
219 font({ weight: "semibold", size: 11 }),
220 foregroundStyle(phaseTint(row.phase)),
221 layoutPriority(1),
222 ]}
223 >
224 {row.status}
225 </Text>
226 </HStack>
227 );
228
229 // The branded T3 mark. `assetName` resolves the template image set bundled in
230 // the widget extension's asset catalog. Image views only honor `resizable`
231 // directly (frame/foregroundStyle are dropped), so we size it via a container
232 // frame the resizable image fills and tint it through the container's
233 // foreground style, which the template image inherits. The 3:2 frame matches
234 // the glyph's aspect ratio so it never distorts.
235 const renderLogo = (height: number, color: Foreground) => (
236 <HStack modifiers={[frame({ width: height * 1.5, height }), foregroundStyle(color)]}>
237 <Image assetName="T3Mark" modifiers={[resizable()]} />
238 </HStack>
239 );
240
241 return {
242 banner: (
243 <VStack
244 alignment="leading"
245 spacing={6}
246 modifiers={[
247 padding({ all: 14 }),
248 // A clear tint reveals iOS 26's glass material; older hosts keep the standard surface.
249 activityBackgroundTint(environment.isLiquidGlassAvailable ? "clear" : null),
250 ...(deepLink ? [widgetURL(deepLink)] : []),
251 ]}
252 >
253 {/* Logo pinned to the leading edge; the status texts centered across the
254 full width (ZStack so the logo doesn't skew the centering). No footer —
255 overflow beyond the visible rows is inferable from the count. */}
256 <ZStack>
257 <HStack spacing={0} alignment="center">
258 {renderLogo(13, primaryForeground)}
259 <Spacer minLength={0} />
260 </HStack>
261 <HStack spacing={6} alignment="center">
262 <Spacer minLength={0} />
263 <Text
264 modifiers={[
265 font({ weight: "semibold", size: 13 }),
266 // The all-done header carries the outcome tint (emerald /
267 // red) the way the Done/Failed status labels do.
268 foregroundStyle(allDone ? headerTint : primaryForeground),
269 lineLimit(1),
270 ]}
271 >
272 {agentsLabel}
273 </Text>
274 {attentionSuffix ? (
275 <Text modifiers={[font({ size: 13 }), foregroundStyle(secondaryForeground)]}>·</Text>
276 ) : null}
277 {attentionSuffix ? (
278 <Text
279 modifiers={[
280 font({ weight: "semibold", size: 13 }),
281 foregroundStyle(headerTint),
282 lineLimit(1),
283 ]}
284 >
285 {attentionSuffix}
286 </Text>
287 ) : null}
288 <Spacer minLength={0} />
289 </HStack>
290 </ZStack>
291 {row0 ? renderCompactRow(row0) : null}
292 {row1 ? renderCompactRow(row1) : null}
293 {row2 ? renderCompactRow(row2) : null}
294 {row3 ? renderCompactRow(row3) : null}
295 {row4 ? renderCompactRow(row4) : null}
296 </VStack>
297 ),
298 // Compact card for the watchOS Smart Stack + CarPlay (the `.small` family):
299 // brand + count, then the single most important agent with its status glyph.
300 bannerSmall: (
301 <VStack alignment="leading" spacing={5} modifiers={[padding({ all: 10 })]}>
302 <HStack spacing={7} alignment="center">
303 {renderLogo(14, primaryForeground)}
304 <Text
305 modifiers={[
306 font({ weight: "bold", size: 13 }),
307 foregroundStyle(headerTint),
308 lineLimit(1),
309 ]}
310 >
311 {attentionRows.length > 0 ? summary : activeLabel}
312 </Text>
313 <Spacer minLength={6} />
314 </HStack>
315 {row0 ? (
316 <HStack spacing={7} alignment="center">
317 <Text
318 modifiers={[
319 font({ weight: "semibold", size: 12 }),
320 foregroundStyle(primaryForeground),
321 lineLimit(1),
322 ]}
323 >
324 {row0.threadTitle}
325 </Text>
326 <Spacer minLength={6} />
327 <Text modifiers={[font({ size: 11 }), foregroundStyle(phaseTint(row0.phase))]}>
328 {row0.status}
329 </Text>
330 </HStack>
331 ) : null}
332 </VStack>
333 ),
334 compactLeading: renderLogo(14, tint),
335 compactTrailing: (
336 <Text modifiers={[font({ weight: "semibold", size: 11 }), foregroundStyle(tint)]}>
337 {attentionRow
338 ? attentionRow.phase === "waiting_for_approval"
339 ? "Approval"
340 : "Input"
341 : activeLabel}
342 </Text>
343 ),
344 // The shared/minimal form is a ~22pt circle — a single signal reads there,
345 // the wordmark does not. Show the blocking/outcome phase glyph, else the
346 // mark (all-done shows the hero row's checkmark/cross).
347 minimal:
348 (attentionRow || failedRow || allDone) && heroRow
349 ? renderGlyph(phaseSymbol(heroRow.phase), 13, phaseTint(heroRow.phase))
350 : renderLogo(11, tint),
351 expandedLeading: (
352 <HStack spacing={5} alignment="center" modifiers={[padding({ leading: 4, vertical: 4 })]}>
353 {renderLogo(15, tint)}
354 <Text modifiers={[font({ weight: "bold", size: 13 }), foregroundStyle(tint)]}>
355 {allDone ? doneLabel : `${props.activeCount}`}
356 </Text>
357 </HStack>
358 ),
359 // No center content: the phase glyphs + statuses in expandedBottom already
360 // carry the attention signal, and the expanded island's height budget is
361 // tight enough that a summary line there pushed the third row off.
362 expandedCenter: null,
363 // No trailing content: a timestamp is glanceable-lock-screen info, not
364 // useful in a view the user is actively holding open — and the trailing
365 // region hugs the island's corner radius, which clipped it anyway.
366 expandedTrailing: null,
367 expandedBottom: (
368 // Vertical padding only: the expanded region provides its own horizontal
369 // content margins, so `all` padding double-indented the rows.
370 // Horizontal padding keeps both edges clear of the island's corner
371 // curvature (right edge clipped status labels; titles hugged the left).
372 <VStack
373 alignment="leading"
374 spacing={5}
375 modifiers={
376 deepLink
377 ? [padding({ vertical: 2, horizontal: 8 }), widgetURL(deepLink)]
378 : [padding({ vertical: 2, horizontal: 8 })]
379 }
380 >
381 {row0 ? renderCompactRow(row0) : null}
382 {row1 ? renderCompactRow(row1) : null}
383 {row2 ? renderCompactRow(row2) : null}
384 </VStack>
385 ),
386 };
387}
388
389export default createLiveActivity<AgentActivityProps>("AgentActivity", AgentActivity);