Open History to see the last commit
1import type {
2 EnvironmentProject,
3 EnvironmentThreadShell,
4} from "@t3tools/client-runtime/state/shell";
5
6import type {
7 AgentActivityPhase,
8 AgentActivityProps,
9 AgentActivityRowProps,
10} from "../../widgets/AgentActivity";
11
12const SHOWCASE_AGENT_ACTIVITY_ROWS = [
13 { threadId: "pocket-command-center", phase: "waiting_for_approval", minutesAgo: 1 },
14 { threadId: "beautiful-boot", phase: "waiting_for_input", minutesAgo: 4 },
15 { threadId: "buttery-suspense", phase: "running", minutesAgo: 2 },
16 { threadId: "remote-command-center", phase: "completed", minutesAgo: 3 },
17] as const satisfies ReadonlyArray<{
18 readonly threadId: string;
19 readonly phase: AgentActivityPhase;
20 readonly minutesAgo: number;
21}>;
22
23const STATUS_BY_PHASE: Record<AgentActivityPhase, string> = {
24 starting: "Connecting",
25 running: "Working",
26 waiting_for_approval: "Approval",
27 waiting_for_input: "Input",
28 stale: "Waiting",
29 completed: "Done",
30 failed: "Failed",
31};
32
33const ACTIVE_PHASES: ReadonlySet<AgentActivityPhase> = new Set([
34 "starting",
35 "running",
36 "waiting_for_approval",
37 "waiting_for_input",
38]);
39
40/** Returns null until every staged thread and its project have loaded. */
41export function buildShowcaseAgentActivity(
42 threads: ReadonlyArray<EnvironmentThreadShell>,
43 projects: ReadonlyArray<EnvironmentProject>,
44 now: number,
45): AgentActivityProps | null {
46 const rows: AgentActivityRowProps[] = [];
47 for (const definition of SHOWCASE_AGENT_ACTIVITY_ROWS) {
48 const thread = threads.find((candidate) => String(candidate.id) === definition.threadId);
49 const project = thread
50 ? projects.find(
51 (candidate) =>
52 candidate.environmentId === thread.environmentId && candidate.id === thread.projectId,
53 )
54 : undefined;
55 if (!thread || !project) return null;
56 const environmentId = String(thread.environmentId);
57 rows.push({
58 environmentId,
59 threadId: definition.threadId,
60 projectTitle: project.title,
61 threadTitle: thread.title,
62 modelTitle: "",
63 phase: definition.phase,
64 status: STATUS_BY_PHASE[definition.phase],
65 updatedAt: new Date(now - definition.minutesAgo * 60_000).toISOString(),
66 deepLink: `/threads/${encodeURIComponent(environmentId)}/${encodeURIComponent(definition.threadId)}`,
67 });
68 }
69 return {
70 title: "T3 Code",
71 subtitle: "Agent work in progress",
72 activeCount: rows.filter((row) => ACTIVE_PHASES.has(row.phase)).length,
73 updatedAt: new Date(now).toISOString(),
74 activities: rows,
75 };
76}
77
78function showcaseAgentAlert(activity: AgentActivityProps) {
79 const row = activity.activities[0];
80 if (!row) return null;
81 return {
82 title: row.threadTitle,
83 body: `${row.status}: ${row.projectTitle}`,
84 path: row.deepLink,
85 environmentId: row.environmentId,
86 threadId: row.threadId,
87 };
88}
89
90/** Matches the relay's androidActivityData FCM map for native rendering. */
91export function showcaseAndroidActivityData(
92 activity: AgentActivityProps,
93 now: number,
94): Record<string, string> {
95 const attentionCount = activity.activities.filter(
96 (row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input",
97 ).length;
98 const hero = activity.activities[0];
99 const alert = showcaseAgentAlert(activity);
100 return {
101 t3_kind: "agent_activity",
102 updated_at: String(now),
103 active: String(activity.activeCount > 0),
104 activity_chip: attentionCount > 0 ? "Review" : "Active",
105 activity_title: `${activity.activeCount} active agents · ${attentionCount} need attention`,
106 activity_phase: hero?.phase ?? "",
107 activity_active_count: String(activity.activeCount),
108 activity_attention_count: String(attentionCount),
109 activity_body: hero ? `${hero.status}: ${hero.threadTitle} · ${hero.projectTitle}` : "",
110 ...Object.fromEntries(
111 activity.activities.map((row, index) => [
112 `activity_line_${index}`,
113 [row.status, row.threadTitle, row.projectTitle].join("\t"),
114 ]),
115 ),
116 activity_path: hero?.deepLink ?? "/",
117 activity_expires_at: String(now + 2 * 60 * 60_000),
118 ...(alert
119 ? {
120 alert_id: "showcase-alert",
121 alert_title: alert.title,
122 alert_body: alert.body,
123 alert_path: alert.path,
124 }
125 : {}),
126 };
127}