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