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/** The expanded Android card uses the same rows and priority as the iOS widget. */
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 activity_title: title,
44 activity_body: hero
45 ? `${hero.status}: ${clean(hero.threadTitle)} · ${clean(hero.projectTitle)}`
46 : "",
47 // Separate keys avoid double JSON encoding and preserve each expanded row when bounding the payload.
48 ...Object.fromEntries(lines.map((line, index) => [`activity_line_${index}`, line])),
49 activity_path: rows[0]?.deepLink ?? "/",
50 activity_expires_at: String(expiresAt),
51 };
52}
53
54/** Keep Unicode, escaping and grouped alerts within FCM's 4 KB data budget. */
55export function fitFcmData(input: Readonly<Record<string, string>>): Record<string, string> {
56 const data = { ...input };
57 const encoder = new TextEncoder();
58 const textKeys = Object.keys(data).filter(
59 (key) => key.endsWith("_body") || key.endsWith("_title") || key.startsWith("activity_line_"),
60 );
61 while (encoder.encode(JSON.stringify(data)).length > 3800) {
62 const key = textKeys.sort(
63 (a, b) => encoder.encode(data[b]!).length - encoder.encode(data[a]!).length,
64 )[0];
65 if (!key) break;
66 if (data[key]!.length <= 8) {
67 textKeys.splice(textKeys.indexOf(key), 1);
68 continue;
69 }
70 const parts = key.startsWith("activity_line_") ? data[key]!.split("\t") : [data[key]!];
71 const part = parts.length === 3 ? (parts[1]!.length > parts[2]!.length ? 1 : 2) : 0;
72 const characters = Array.from(parts[part]!);
73 // Five characters would become four plus the ellipsis and never shrink.
74 if (characters.length <= 5) {
75 textKeys.splice(textKeys.indexOf(key), 1);
76 continue;
77 }
78 parts[part] =
79 characters
80 .slice(0, Math.floor(characters.length * 0.8))
81 .join("")
82 .trimEnd() + "…";
83 data[key] = parts.join("\t");
84 }
85 return data;
86}