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
12export const 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
23// Matches the relay's row wording (AgentActivityPublisher.statusForPhase).
24const STATUS_BY_PHASE: Record<AgentActivityPhase, string> = {
25 starting: "Connecting",
26 running: "Working",
27 waiting_for_approval: "Approval",
28 waiting_for_input: "Input",
29 stale: "Waiting",
30 completed: "Done",
31 failed: "Failed",
32};
33
34const ACTIVE_PHASES: ReadonlySet<AgentActivityPhase> = new Set([
35 "starting",
36 "running",
37 "waiting_for_approval",
38 "waiting_for_input",
39]);
40
41/** Returns null until every staged thread and its project have loaded. */
42export function buildShowcaseAgentActivity(
43 threads: ReadonlyArray<EnvironmentThreadShell>,
44 projects: ReadonlyArray<EnvironmentProject>,
45 now: number,
46): AgentActivityProps | null {
47 const rows: AgentActivityRowProps[] = [];
48 for (const definition of SHOWCASE_AGENT_ACTIVITY_ROWS) {
49 const thread = threads.find((candidate) => String(candidate.id) === definition.threadId);
50 const project = thread
51 ? projects.find(
52 (candidate) =>
53 candidate.environmentId === thread.environmentId && candidate.id === thread.projectId,
54 )
55 : undefined;
56 if (!thread || !project) return null;
57 const environmentId = String(thread.environmentId);
58 rows.push({
59 environmentId,
60 threadId: definition.threadId,
61 projectTitle: project.title,
62 threadTitle: thread.title,
63 modelTitle: "",
64 phase: definition.phase,
65 status: STATUS_BY_PHASE[definition.phase],
66 updatedAt: new Date(now - definition.minutesAgo * 60_000).toISOString(),
67 deepLink: `/threads/${encodeURIComponent(environmentId)}/${encodeURIComponent(definition.threadId)}`,
68 });
69 }
70 return {
71 title: "T3 Code",
72 subtitle: "Agent work in progress",
73 activeCount: rows.filter((row) => ACTIVE_PHASES.has(row.phase)).length,
74 updatedAt: new Date(now).toISOString(),
75 activities: rows,
76 };
77}
78
79export function showcaseAgentAlert(activity: AgentActivityProps) {
80 const row = activity.activities[0];
81 if (!row) return null;
82 return {
83 title: row.threadTitle,
84 body: `${row.status}: ${row.projectTitle}`,
85 path: row.deepLink,
86 environmentId: row.environmentId,
87 threadId: row.threadId,
88 };
89}
90
91/**
92 * The FCM data map Android's native receiver renders, mirroring the relay's
93 * androidActivityData so the shade shows exactly what a real push produces.
94 */
95export function showcaseAndroidActivityData(
96 activity: AgentActivityProps,
97 now: number,
98): Record<string, string> {
99 const attentionCount = activity.activities.filter(
100 (row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input",
101 ).length;
102 const hero = activity.activities[0];
103 const alert = showcaseAgentAlert(activity);
104 return {
105 t3_kind: "agent_activity",
106 updated_at: String(now),
107 active: String(activity.activeCount > 0),
108 activity_chip: attentionCount > 0 ? "Review" : "Active",
109 activity_title: `${activity.activeCount} active agents · ${attentionCount} need attention`,
110 activity_phase: hero?.phase ?? "",
111 activity_active_count: String(activity.activeCount),
112 activity_attention_count: String(attentionCount),
113 activity_body: hero ? `${hero.status}: ${hero.threadTitle} · ${hero.projectTitle}` : "",
114 ...Object.fromEntries(
115 activity.activities.map((row, index) => [
116 `activity_line_${index}`,
117 [row.status, row.threadTitle, row.projectTitle].join("\t"),
118 ]),
119 ),
120 activity_path: hero?.deepLink ?? "/",
121 activity_expires_at: String(now + 2 * 60 * 60_000),
122 ...(alert
123 ? {
124 alert_id: "showcase-alert",
125 alert_title: alert.title,
126 alert_body: alert.body,
127 alert_path: alert.path,
128 }
129 : {}),
130 };
131}