Open History to see the last commit
1import type {
2 RelayAgentActivityAggregateRow,
3 RelayAgentActivityAggregateState,
4 RelayAgentActivityState,
5} from "@t3tools/contracts/relay";
6import * as DateTime from "effect/DateTime";
7import * as Option from "effect/Option";
8
9import type { ApnsNotificationPayload } from "./apnsDeliveryJobs.ts";
10
11export function isTerminalPhase(state: RelayAgentActivityState): boolean {
12 return state.phase === "completed" || state.phase === "failed";
13}
14
15// Rows are only removed when their environment publishes a terminal state. An
16// environment that dies mid-run (machine off, process killed) never does, so
17// without an age cutoff its threads inflate activeCount forever.
18const RUNNING_AGENT_ACTIVITY_ROW_TTL_MS = 2 * 60 * 60 * 1_000;
19const WAITING_AGENT_ACTIVITY_ROW_TTL_MS = 24 * 60 * 60 * 1_000;
20
21export function agentActivityExpiresAt(
22 state: Pick<RelayAgentActivityState, "phase" | "updatedAt">,
23): number {
24 const updatedAtMs = Option.match(DateTime.make(state.updatedAt), {
25 onNone: () => Number.NaN,
26 onSome: (dt) => dt.epochMilliseconds,
27 });
28 if (Number.isNaN(updatedAtMs)) {
29 return Number.NaN;
30 }
31 const ttlMs =
32 state.phase === "running" || state.phase === "starting"
33 ? RUNNING_AGENT_ACTIVITY_ROW_TTL_MS
34 : WAITING_AGENT_ACTIVITY_ROW_TTL_MS;
35 return updatedAtMs + ttlMs;
36}
37
38export function isExpiredAgentActivityState(
39 state: RelayAgentActivityState,
40 nowMs: number,
41): boolean {
42 const expiresAt = agentActivityExpiresAt(state);
43 return !Number.isFinite(expiresAt) || nowMs > expiresAt;
44}
45
46const MAX_SUMMARY_TEXT_LENGTH = 120;
47const MAX_STATUS_TEXT_LENGTH = 40;
48const MAX_DEEP_LINK_LENGTH = 512;
49// The Live Activity banner (lock screen / Notification Center) renders up to
50// five rows; the expanded Dynamic Island shows the top three of these.
51export const MAX_ACTIVITY_ROWS = 5;
52
53function truncateText(value: string, maxLength: number): string {
54 const trimmed = value.trim();
55 if (trimmed.length <= maxLength) {
56 return trimmed;
57 }
58 return trimmed.slice(0, maxLength - 3).trimEnd() + "...";
59}
60
61function sanitizeDeepLink(value: string): string {
62 const trimmed = value.trim();
63 if (!trimmed.startsWith("/") || trimmed.startsWith("//")) {
64 return "/";
65 }
66 return truncateText(trimmed, MAX_DEEP_LINK_LENGTH);
67}
68
69export function sanitizeAgentActivityAggregateRow(
70 row: RelayAgentActivityAggregateRow,
71): RelayAgentActivityAggregateRow {
72 return {
73 ...row,
74 projectTitle: truncateText(row.projectTitle, MAX_SUMMARY_TEXT_LENGTH),
75 threadTitle: truncateText(row.threadTitle, MAX_SUMMARY_TEXT_LENGTH),
76 modelTitle: truncateText(row.modelTitle, MAX_SUMMARY_TEXT_LENGTH),
77 status: truncateText(row.status, MAX_STATUS_TEXT_LENGTH),
78 deepLink: sanitizeDeepLink(row.deepLink),
79 };
80}
81
82export function sanitizeAgentActivityAggregateState(
83 aggregate: RelayAgentActivityAggregateState,
84): RelayAgentActivityAggregateState {
85 return {
86 ...aggregate,
87 title: truncateText(aggregate.title, MAX_SUMMARY_TEXT_LENGTH),
88 subtitle: truncateText(aggregate.subtitle, MAX_SUMMARY_TEXT_LENGTH),
89 activities: aggregate.activities
90 .slice(0, MAX_ACTIVITY_ROWS)
91 .map(sanitizeAgentActivityAggregateRow),
92 };
93}
94
95export function sanitizeApnsNotificationPayload(
96 notification: ApnsNotificationPayload,
97): ApnsNotificationPayload {
98 return {
99 ...notification,
100 title: truncateText(notification.title, MAX_SUMMARY_TEXT_LENGTH),
101 body: truncateText(notification.body, MAX_SUMMARY_TEXT_LENGTH),
102 deepLink: sanitizeDeepLink(notification.deepLink),
103 };
104}
105
106export function notificationForActivity(
107 row: RelayAgentActivityAggregateRow,
108): ApnsNotificationPayload {
109 const activity = sanitizeAgentActivityAggregateRow(row);
110 return sanitizeApnsNotificationPayload({
111 title: activity.threadTitle,
112 body: `${activity.status}: ${activity.projectTitle}`,
113 environmentId: activity.environmentId,
114 threadId: activity.threadId,
115 deepLink: activity.deepLink,
116 phase: activity.phase,
117 updatedAt: activity.updatedAt,
118 });
119}