@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 119 lines 4.22 KB
Raw
1 import type { RelayAgentActivityAggregateState } from "@t3tools/contracts/relay";
2 import { msg } from "gt-react-native";
3
4 import { m, type MessageResolver } from "../i18n/messages";
5
6 import type { AgentActivityProps, AgentActivityText } from "./AgentActivity";
7
8 // The widget only refreshes while the app is open, so after this long it stops
9 // claiming an agent is still working. Matches the Live Activity's stale date.
10 export const AGENT_ACTIVITY_WIDGET_STALE_AFTER_MS = 10 * 60_000;
11
12 export type AgentActivityWidgetSource =
13 | { readonly kind: "signed-out" }
14 | { readonly kind: "unavailable" }
15 | { readonly kind: "loading" }
16 | { readonly kind: "snapshot"; readonly aggregate: RelayAgentActivityAggregateState | null };
17
18 function countTable(maxCount: number, format: (count: number) => string) {
19 return Object.fromEntries(
20 Array.from({ length: maxCount }, (_, index) => [String(index + 1), format(index + 1)]),
21 );
22 }
23
24 /** Translated widget copy, with count tables for counts up to `maxCount`. */
25 export function agentActivityText(
26 maxCount: number,
27 resolve: MessageResolver = m,
28 ): AgentActivityText {
29 return {
30 outOfDate: resolve(msg("Out of date")),
31 failed: resolve(msg("Failed", { $context: "agent work status" })),
32 done: resolve(msg("Done", { $context: "agent work status" })),
33 workFailed: resolve(msg("Agent work failed")),
34 workCompleted: resolve(msg("Agent work completed")),
35 statusOutOfDate: resolve(msg("Agent status out of date")),
36 approval: resolve(msg("Approval", { $context: "agent waits for an approval" })),
37 input: resolve(msg("Input", { $context: "agent waits for user input" })),
38 noActiveAgents: resolve(msg("No active agents")),
39 activeAgents: countTable(maxCount, (count) =>
40 resolve(
41 msg("{count, plural, one {{count} active agent} other {{count} active agents}}", {
42 count,
43 }),
44 ),
45 ),
46 active: countTable(maxCount, (count) =>
47 resolve(
48 msg("{count, plural, one {{count} active} other {{count} active}}", {
49 count,
50 $context: "number of agents working",
51 }),
52 ),
53 ),
54 needAttention: countTable(maxCount, (count) =>
55 resolve(
56 msg("{count, plural, one {{count} needs attention} other {{count} need attention}}", {
57 count,
58 }),
59 ),
60 ),
61 needYou: countTable(maxCount, (count) =>
62 resolve(msg("{count, plural, one {{count} needs you} other {{count} need you}}", { count })),
63 ),
64 };
65 }
66
67 export function agentActivityWidgetTimeline(
68 source: AgentActivityWidgetSource,
69 now: number,
70 resolve: MessageResolver = m,
71 ) {
72 const updatedAt = new Date(now).toISOString();
73 if (source.kind !== "snapshot" || !source.aggregate) {
74 const props: AgentActivityProps = {
75 text: agentActivityText(0, resolve),
76 title: "T3 Code",
77 subtitle:
78 source.kind === "signed-out"
79 ? resolve(msg("Sign in to {product} to see agents", { product: "T3 Connect" }))
80 : source.kind === "unavailable"
81 ? resolve(msg("Couldn't reach {product}", { product: "T3 Connect" }))
82 : source.kind === "loading"
83 ? resolve(msg("Loading agents…"))
84 : resolve(msg("No active agents")),
85 activeCount: 0,
86 updatedAt,
87 activities: [],
88 };
89 return [{ date: new Date(now), props }];
90 }
91
92 const { aggregate } = source;
93 const props: AgentActivityProps = {
94 text: agentActivityText(Math.max(aggregate.activeCount, aggregate.activities.length), resolve),
95 title: aggregate.title,
96 subtitle: aggregate.subtitle,
97 activeCount: aggregate.activeCount,
98 updatedAt: aggregate.updatedAt,
99 activities: aggregate.activities,
100 };
101 const hasInFlightRows = props.activities.some(
102 (row) => row.phase !== "completed" && row.phase !== "failed",
103 );
104 if (!hasInFlightRows) {
105 return [{ date: new Date(now), props }];
106 }
107 const staleProps: AgentActivityProps = {
108 ...props,
109 activities: props.activities.map((row) =>
110 row.phase === "completed" || row.phase === "failed"
111 ? row
112 : { ...row, phase: "stale", status: resolve(msg("Out of date")) },
113 ),
114 };
115 return [
116 { date: new Date(now), props },
117 { date: new Date(now + AGENT_ACTIVITY_WIDGET_STALE_AFTER_MS), props: staleProps },
118 ];
119 }