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

AgentActivity.tsx

376 lines12.5 KB

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;
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>;
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 const linkModifiers = deepLink ? [widgetURL(deepLink)] : [];
162​
163 type SFName = NonNullable<ComponentProps<typeof Image>["systemName"]>;
164 const phaseSymbol = (phase: AgentActivityPhase): SFName => {
165 switch (phase) {
166 case "waiting_for_approval":
167 return "exclamationmark.circle.fill";
168 case "waiting_for_input":
169 return "questionmark.circle.fill";
170 case "failed":
171 return "xmark.octagon.fill";
172 case "completed":
173 return "checkmark.circle.fill";
174 case "starting":
175 return "circle.dotted";
176 case "stale":
177 return "clock.arrow.circlepath";
178 case "running":
179 default:
180 return "arrow.triangle.2.circlepath";
181 }
182 };
183​
184 // SF Symbols, like the logo, ignore frame/foregroundStyle applied directly to
185 // the image; size + tint them through a container the resizable symbol fills.
186 const renderGlyph = (
187 systemName: SFName,
188 size: number,
189 color: Foreground,
190 modifiers: typeof linkModifiers = [],
191 ) => (
192 <HStack
193 modifiers={[frame({ width: size, height: size }), foregroundStyle(color), ...modifiers]}
194 >
195 <Image systemName={systemName} modifiers={[resizable()]} />
196 </HStack>
197 );
198​
199 const renderCompactRow = (row: AgentActivityRowProps) => (
200 <HStack spacing={7} alignment="center">
201 <Text
202 modifiers={[
203 font({ weight: "semibold", size: 13 }),
204 foregroundStyle(primaryForeground),
205 lineLimit(1),
206 ]}
207 >
208 {row.threadTitle}
209 </Text>
210 {/* No layoutPriority and no frame on the project: two bare texts take
211 their ideal width when it fits and shrink proportionally only when it
212 doesn't — so short rows never truncate, and long title + long project
213 truncate together. */}
214 <Text modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}>
215 {row.projectTitle}
216 </Text>
217 <Spacer minLength={8} />
218 <Text
219 modifiers={[
220 font({ weight: "semibold", size: 11 }),
221 foregroundStyle(phaseTint(row.phase)),
222 layoutPriority(1),
223 ]}
224 >
225 {row.status}
226 </Text>
227 </HStack>
228 );
229​
230 // Image views only honor `resizable` directly (frame/foregroundStyle are
231 // dropped), so we size it via a container frame the resizable image fills and
232 // tint it through the container's foreground style, which the template image
233 // inherits.
234 const renderLogo = (height: number, color: Foreground, modifiers: typeof linkModifiers = []) => (
235 <HStack
236 modifiers={[frame({ width: height * 1.5, height }), foregroundStyle(color), ...modifiers]}
237 >
238 <Image assetName="T3Mark" modifiers={[resizable()]} />
239 </HStack>
240 );
241​
242 return {
243 banner: (
244 <VStack
245 alignment="leading"
246 spacing={6}
247 modifiers={[
248 padding({ all: 14 }),
249 // A clear tint reveals iOS 26's glass material; older hosts keep the standard surface.
250 activityBackgroundTint(environment.isLiquidGlassAvailable ? "clear" : null),
251 ...(deepLink ? [widgetURL(deepLink)] : []),
252 ]}
253 >
254 <ZStack>
255 <HStack spacing={0} alignment="center">
256 {renderLogo(13, primaryForeground)}
257 <Spacer minLength={0} />
258 </HStack>
259 <HStack spacing={6} alignment="center">
260 <Spacer minLength={0} />
261 <Text
262 modifiers={[
263 font({ weight: "semibold", size: 13 }),
264 foregroundStyle(allDone ? headerTint : primaryForeground),
265 lineLimit(1),
266 ]}
267 >
268 {agentsLabel}
269 </Text>
270 {attentionSuffix ? (
271 <Text modifiers={[font({ size: 13 }), foregroundStyle(secondaryForeground)]}>·</Text>
272 ) : null}
273 {attentionSuffix ? (
274 <Text
275 modifiers={[
276 font({ weight: "semibold", size: 13 }),
277 foregroundStyle(headerTint),
278 lineLimit(1),
279 ]}
280 >
281 {attentionSuffix}
282 </Text>
283 ) : null}
284 <Spacer minLength={0} />
285 </HStack>
286 </ZStack>
287 {row0 ? renderCompactRow(row0) : null}
288 {row1 ? renderCompactRow(row1) : null}
289 {row2 ? renderCompactRow(row2) : null}
290 {row3 ? renderCompactRow(row3) : null}
291 {row4 ? renderCompactRow(row4) : null}
292 </VStack>
293 ),
294 bannerSmall: (
295 <VStack alignment="leading" spacing={5} modifiers={[padding({ all: 10 })]}>
296 <HStack spacing={7} alignment="center">
297 {renderLogo(14, primaryForeground)}
298 <Text
299 modifiers={[
300 font({ weight: "bold", size: 13 }),
301 foregroundStyle(headerTint),
302 lineLimit(1),
303 ]}
304 >
305 {attentionRows.length > 0 ? summary : activeLabel}
306 </Text>
307 <Spacer minLength={6} />
308 </HStack>
309 {row0 ? (
310 <HStack spacing={7} alignment="center">
311 <Text
312 modifiers={[
313 font({ weight: "semibold", size: 12 }),
314 foregroundStyle(primaryForeground),
315 lineLimit(1),
316 ]}
317 >
318 {row0.threadTitle}
319 </Text>
320 <Spacer minLength={6} />
321 <Text modifiers={[font({ size: 11 }), foregroundStyle(phaseTint(row0.phase))]}>
322 {row0.status}
323 </Text>
324 </HStack>
325 ) : null}
326 </VStack>
327 ),
328 compactLeading: renderLogo(14, tint, linkModifiers),
329 compactTrailing: (
330 <Text
331 modifiers={[
332 font({ weight: "semibold", size: 11 }),
333 foregroundStyle(tint),
334 ...linkModifiers,
335 ]}
336 >
337 {attentionRow
338 ? attentionRow.phase === "waiting_for_approval"
339 ? "Approval"
340 : "Input"
341 : activeLabel}
342 </Text>
343 ),
344 minimal:
345 (attentionRow || failedRow || allDone) && heroRow
346 ? renderGlyph(phaseSymbol(heroRow.phase), 13, phaseTint(heroRow.phase), linkModifiers)
347 : renderLogo(11, tint, linkModifiers),
348 expandedLeading: (
349 <HStack spacing={5} alignment="center" modifiers={[padding({ leading: 4, vertical: 4 })]}>
350 {renderLogo(15, tint)}
351 <Text modifiers={[font({ weight: "bold", size: 13 }), foregroundStyle(tint)]}>
352 {allDone ? doneLabel : `${props.activeCount}`}
353 </Text>
354 </HStack>
355 ),
356 expandedCenter: null,
357 expandedTrailing: null,
358 expandedBottom: (
359 <VStack
360 alignment="leading"
361 spacing={5}
362 modifiers={
363 deepLink
364 ? [padding({ vertical: 2, horizontal: 8 }), widgetURL(deepLink)]
365 : [padding({ vertical: 2, horizontal: 8 })]
366 }
367 >
368 {row0 ? renderCompactRow(row0) : null}
369 {row1 ? renderCompactRow(row1) : null}
370 {row2 ? renderCompactRow(row2) : null}
371 </VStack>
372 ),
373 };
375​
376export default createLiveActivity<AgentActivityProps>("AgentActivity", AgentActivity);