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// Dead environments never publish terminal states; expire their rows so activeCount cannot remain inflated.
16const RUNNING_AGENT_ACTIVITY_ROW_TTL_MS = 2 * 60 * 60 * 1_000;
17const WAITING_AGENT_ACTIVITY_ROW_TTL_MS = 24 * 60 * 60 * 1_000;
18
19export function agentActivityExpiresAt(
20 state: Pick<RelayAgentActivityState, "phase" | "updatedAt">,
21): number {
22 const updatedAtMs = Option.match(DateTime.make(state.updatedAt), {
23 onNone: () => Number.NaN,
24 onSome: (dt) => dt.epochMilliseconds,
25 });
26 if (Number.isNaN(updatedAtMs)) {
27 return Number.NaN;
28 }
29 const ttlMs =
30 state.phase === "running" || state.phase === "starting"
31 ? RUNNING_AGENT_ACTIVITY_ROW_TTL_MS
32 : WAITING_AGENT_ACTIVITY_ROW_TTL_MS;
33 return updatedAtMs + ttlMs;
34}
35
36export function isExpiredAgentActivityState(
37 state: RelayAgentActivityState,
38 nowMs: number,
39): boolean {
40 const expiresAt = agentActivityExpiresAt(state);
41 return !Number.isFinite(expiresAt) || nowMs > expiresAt;
42}
43
44export const MAX_SUMMARY_TEXT_LENGTH = 120;
45const MAX_STATUS_TEXT_LENGTH = 40;
46const MAX_DEEP_LINK_LENGTH = 512;
47// Banners render five rows; the expanded Dynamic Island shows the top three.
48export const MAX_ACTIVITY_ROWS = 5;
49
50export function truncateText(value: string, maxLength: number): string {
51 const trimmed = value.trim();
52 if (trimmed.length <= maxLength) {
53 return trimmed;
54 }
55 return (
56 trimmed
57 .slice(0, maxLength - 3)
58 .replace(/[\uD800-\uDBFF]$/, "")
59 .trimEnd() + "..."
60 );
61}
62
63function sanitizeDeepLink(value: string): string {
64 const trimmed = value.trim();
65 if (!trimmed.startsWith("/") || trimmed.startsWith("//")) {
66 return "/";
67 }
68 return truncateText(trimmed, MAX_DEEP_LINK_LENGTH);
69}
70
71function sanitizeAgentActivityAggregateRow(
72 row: RelayAgentActivityAggregateRow,
73): RelayAgentActivityAggregateRow {
74 return {
75 ...row,
76 projectTitle: truncateText(row.projectTitle, MAX_SUMMARY_TEXT_LENGTH),
77 threadTitle: truncateText(row.threadTitle, MAX_SUMMARY_TEXT_LENGTH),
78 modelTitle: truncateText(row.modelTitle, MAX_SUMMARY_TEXT_LENGTH),
79 status: truncateText(row.status, MAX_STATUS_TEXT_LENGTH),
80 deepLink: sanitizeDeepLink(row.deepLink),
81 };
82}
83
84export function sanitizeAgentActivityAggregateState(
85 aggregate: RelayAgentActivityAggregateState,
86): RelayAgentActivityAggregateState {
87 return {
88 ...aggregate,
89 title: truncateText(aggregate.title, MAX_SUMMARY_TEXT_LENGTH),
90 subtitle: truncateText(aggregate.subtitle, MAX_SUMMARY_TEXT_LENGTH),
91 activities: aggregate.activities
92 .slice(0, MAX_ACTIVITY_ROWS)
93 .map(sanitizeAgentActivityAggregateRow),
94 };
95}
96
97export function sanitizeApnsNotificationPayload(
98 notification: ApnsNotificationPayload,
99): ApnsNotificationPayload {
100 return {
101 ...notification,
102 title: truncateText(notification.title, MAX_SUMMARY_TEXT_LENGTH),
103 body: truncateText(notification.body, MAX_SUMMARY_TEXT_LENGTH),
104 deepLink: sanitizeDeepLink(notification.deepLink),
105 };
106}
107
108export function notificationForActivity(
109 row: RelayAgentActivityAggregateRow,
110): ApnsNotificationPayload {
111 const activity = sanitizeAgentActivityAggregateRow(row);
112 return sanitizeApnsNotificationPayload({
113 title: activity.threadTitle,
114 body: `${activity.status}: ${activity.projectTitle}`,
115 environmentId: activity.environmentId,
116 threadId: activity.threadId,
117 deepLink: activity.deepLink,
118 phase: activity.phase,
119 updatedAt: activity.updatedAt,
120 });
121}