@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 116 lines 3.76 KB
Raw
1 import type {
2 RelayAgentActivityAggregateRow,
3 RelayAgentActivityAggregateState,
4 RelayAgentActivityState,
5 } from "@t3tools/contracts/relay";
6 import * as DateTime from "effect/DateTime";
7 import * as Option from "effect/Option";
8
9 import type { ApnsNotificationPayload } from "./apnsDeliveryJobs.ts";
10
11 export 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.
16 const RUNNING_AGENT_ACTIVITY_ROW_TTL_MS = 2 * 60 * 60 * 1_000;
17 const WAITING_AGENT_ACTIVITY_ROW_TTL_MS = 24 * 60 * 60 * 1_000;
18
19 export 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
36 export function isExpiredAgentActivityState(
37 state: RelayAgentActivityState,
38 nowMs: number,
39 ): boolean {
40 const expiresAt = agentActivityExpiresAt(state);
41 return !Number.isFinite(expiresAt) || nowMs > expiresAt;
42 }
43
44 export const MAX_SUMMARY_TEXT_LENGTH = 120;
45 const MAX_STATUS_TEXT_LENGTH = 40;
46 const MAX_DEEP_LINK_LENGTH = 512;
47 // Banners render five rows; the expanded Dynamic Island shows the top three.
48 export const MAX_ACTIVITY_ROWS = 5;
49
50 export function 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() + "...";
56 }
57
58 function 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);
64 }
65
66 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 };
77 }
78
79 export 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 };
90 }
91
92 export 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 };
101 }
102
103 export function notificationForActivity(
104 row: RelayAgentActivityAggregateRow,
105 ): ApnsNotificationPayload {
106 const activity = sanitizeAgentActivityAggregateRow(row);
107 return sanitizeApnsNotificationPayload({
108 title: activity.threadTitle,
109 body: `${activity.status}: ${activity.projectTitle}`,
110 environmentId: activity.environmentId,
111 threadId: activity.threadId,
112 deepLink: activity.deepLink,
113 phase: activity.phase,
114 updatedAt: activity.updatedAt,
115 });
116 }