Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

agentActivityPayloads.ts

101 lines3.5 KB

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";
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. Actively
18// running phases expire quickly; waiting phases can legitimately sit for hours
19// while a user ignores an approval prompt, so they get a longer window. The
20// underlying database row is left in place: a late publish for the thread
21// refreshes updatedAt and the row becomes visible again.
22const RUNNING_AGENT_ACTIVITY_ROW_TTL_MS = 2 * 60 * 60 * 1_000;
23const WAITING_AGENT_ACTIVITY_ROW_TTL_MS = 24 * 60 * 60 * 1_000;
24​
25export function isExpiredAgentActivityState(
26 state: RelayAgentActivityState,
27 nowMs: number,
28): boolean {
29 const updatedAtMs = Option.match(DateTime.make(state.updatedAt), {
30 onNone: () => Number.NaN,
31 onSome: (dt) => dt.epochMilliseconds,
32 });
33 if (Number.isNaN(updatedAtMs)) {
34 return true;
35 }
36 const ttlMs =
37 state.phase === "running" || state.phase === "starting"
38 ? RUNNING_AGENT_ACTIVITY_ROW_TTL_MS
39 : WAITING_AGENT_ACTIVITY_ROW_TTL_MS;
40 return nowMs - updatedAtMs > ttlMs;
42​
43const MAX_SUMMARY_TEXT_LENGTH = 120;
44const MAX_STATUS_TEXT_LENGTH = 40;
45const MAX_DEEP_LINK_LENGTH = 512;
46// The Live Activity banner (lock screen / Notification Center) renders up to
47// five rows; the expanded Dynamic Island shows the top three of these.
48export const MAX_ACTIVITY_ROWS = 5;
49​
50function truncateText(value: string, maxLength: number): string {
51 const trimmed = value.trim();
52 if (trimmed.length <= maxLength) {
53 return trimmed;
54 }
55 return trimmed.slice(0, maxLength - 3).trimEnd() + "...";
57​
58function sanitizeDeepLink(value: string): string {
59 const trimmed = value.trim();
60 if (!trimmed.startsWith("/") || trimmed.startsWith("//")) {
61 return "/";
62 }
63 return truncateText(trimmed, MAX_DEEP_LINK_LENGTH);
65​
66export function sanitizeAgentActivityAggregateRow(
67 row: RelayAgentActivityAggregateRow,
68): RelayAgentActivityAggregateRow {
69 return {
70 ...row,
71 projectTitle: truncateText(row.projectTitle, MAX_SUMMARY_TEXT_LENGTH),
72 threadTitle: truncateText(row.threadTitle, MAX_SUMMARY_TEXT_LENGTH),
73 modelTitle: truncateText(row.modelTitle, MAX_SUMMARY_TEXT_LENGTH),
74 status: truncateText(row.status, MAX_STATUS_TEXT_LENGTH),
75 deepLink: sanitizeDeepLink(row.deepLink),
76 };
78​
79export function sanitizeAgentActivityAggregateState(
80 aggregate: RelayAgentActivityAggregateState,
81): RelayAgentActivityAggregateState {
82 return {
83 ...aggregate,
84 title: truncateText(aggregate.title, MAX_SUMMARY_TEXT_LENGTH),
85 subtitle: truncateText(aggregate.subtitle, MAX_SUMMARY_TEXT_LENGTH),
86 activities: aggregate.activities
87 .slice(0, MAX_ACTIVITY_ROWS)
88 .map(sanitizeAgentActivityAggregateRow),
89 };
91​
92export function sanitizeApnsNotificationPayload(
93 notification: ApnsNotificationPayload,
94): ApnsNotificationPayload {
95 return {
96 ...notification,
97 title: truncateText(notification.title, MAX_SUMMARY_TEXT_LENGTH),
98 body: truncateText(notification.body, MAX_SUMMARY_TEXT_LENGTH),
99 deepLink: sanitizeDeepLink(notification.deepLink),
100 };