The best way to control Claude Code, Codex and more from any computer or phone

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