Open History to see the last commit
1import type { RelayAgentActivityAggregateState } from "@t3tools/contracts/relay";
2import {
3 activityPhasePriority,
4 TERMINAL_AGENT_ACTIVITY_DISPLAY_TTL_MS,
5} from "./agentActivityAggregate.ts";
6import { agentActivityExpiresAt } from "./agentActivityPayloads.ts";
7
8export function androidActivityHero(aggregate: RelayAgentActivityAggregateState) {
9 return [...aggregate.activities].sort(
10 (a, b) => activityPhasePriority(a.phase) - activityPhasePriority(b.phase),
11 )[0];
12}
13
14/** Android focuses the priority thread while retaining rows for older clients. */
15export function androidActivityData(aggregate: RelayAgentActivityAggregateState | null) {
16 const rows = [...(aggregate?.activities ?? [])].sort(
17 (a, b) => activityPhasePriority(a.phase) - activityPhasePriority(b.phase),
18 );
19 const activeCount = aggregate?.activeCount ?? 0;
20 const attentionCount = rows.filter((row) => activityPhasePriority(row.phase) === 0).length;
21 const failed = rows.some((row) => row.phase === "failed");
22 const clean = (value: string) => value.replace(/\s+/g, " ").trim();
23 const lines = rows.map((row) =>
24 [row.status, clean(row.threadTitle), clean(row.projectTitle)].join("\t"),
25 );
26 const hero = rows[0];
27 const title =
28 activeCount > 0
29 ? `${activeCount} active agent${activeCount === 1 ? "" : "s"}${attentionCount ? ` · ${attentionCount} need${attentionCount === 1 ? "s" : ""} attention` : ""}`
30 : failed
31 ? "Agent work failed"
32 : "Agent work completed";
33 const expiresAt = Math.max(
34 0,
35 ...rows.map((row) =>
36 row.phase === "completed" || row.phase === "failed"
37 ? Date.parse(row.updatedAt) + TERMINAL_AGENT_ACTIVITY_DISPLAY_TTL_MS
38 : agentActivityExpiresAt(row),
39 ),
40 );
41 return {
42 active: String(activeCount > 0),
43 // Keep the status bar chip short enough to display alongside the app icon.
44 activity_chip: activeCount > 0 ? (attentionCount > 0 ? "Review" : "Active") : "",
45 activity_title: title,
46 activity_phase: hero?.phase ?? "",
47 activity_active_count: String(activeCount),
48 activity_attention_count: String(attentionCount),
49 activity_body: hero
50 ? `${hero.status}: ${clean(hero.threadTitle)} · ${clean(hero.projectTitle)}`
51 : "",
52 // Separate keys avoid double JSON encoding and preserve each expanded row when bounding the payload.
53 ...Object.fromEntries(lines.map((line, index) => [`activity_line_${index}`, line])),
54 activity_path: rows[0]?.deepLink ?? "/",
55 activity_expires_at: String(expiresAt),
56 };
57}
58
59/** Keep Unicode, escaping and grouped alerts within FCM's 4 KB data budget. */
60export function fitFcmData(input: Readonly<Record<string, string>>): Record<string, string> {
61 const data = { ...input };
62 const encoder = new TextEncoder();
63 const textKeys = Object.keys(data).filter(
64 (key) => key.endsWith("_body") || key.endsWith("_title") || key.startsWith("activity_line_"),
65 );
66 while (encoder.encode(JSON.stringify(data)).length > 3800) {
67 const key = textKeys.sort(
68 (a, b) => encoder.encode(data[b]!).length - encoder.encode(data[a]!).length,
69 )[0];
70 if (!key) break;
71 if (data[key]!.length <= 8) {
72 textKeys.splice(textKeys.indexOf(key), 1);
73 continue;
74 }
75 const parts = key.startsWith("activity_line_") ? data[key]!.split("\t") : [data[key]!];
76 const part = parts.length === 3 ? (parts[1]!.length > parts[2]!.length ? 1 : 2) : 0;
77 const characters = Array.from(parts[part]!);
78 // Five characters would become four plus the ellipsis and never shrink.
79 if (characters.length <= 5) {
80 textKeys.splice(textKeys.indexOf(key), 1);
81 continue;
82 }
83 parts[part] =
84 characters
85 .slice(0, Math.floor(characters.length * 0.8))
86 .join("")
87 .trimEnd() + "…";
88 data[key] = parts.join("\t");
89 }
90 return data;
91}