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

agentActivityWidgetTimeline.ts

138 lines4.5 KB

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