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

agentActivityAlerts.ts

150 lines5.0 KB

1import type {
2 RelayAgentActivityAggregateRow,
3 RelayAgentActivityAggregateState,
4 RelayAgentAwarenessPreferences,
5} from "@t3tools/contracts/relay";
6import * as DateTime from "effect/DateTime";
7import * as Option from "effect/Option";
8​
9export interface AgentActivityAlert {
10 readonly title: string;
11 readonly body: string;
13​
14export const TERMINAL_NOTIFICATION_FRESHNESS_MS = 2 * 60 * 1_000;
15​
16export function isFreshTerminalNotification(updatedAt: string, nowMs: number): boolean {
17 const timestamp = Option.getOrNull(DateTime.make(updatedAt));
18 return (
19 timestamp !== null && nowMs - timestamp.epochMilliseconds <= TERMINAL_NOTIFICATION_FRESHNESS_MS
20 );
22​
23type TransitionInput = {
24 readonly previousAggregate: RelayAgentActivityAggregateState | null;
25 readonly nextAggregate: RelayAgentActivityAggregateState;
26 readonly preferences: RelayAgentAwarenessPreferences | null;
27};
28​
29function rowKey(row: RelayAgentActivityAggregateRow): string {
30 return JSON.stringify([row.environmentId, row.threadId]);
32​
33function isAttentionPhase(phase: string): boolean {
34 return phase === "waiting_for_approval" || phase === "waiting_for_input";
36​
37export function alertAllowedForPhase(
38 preferences: RelayAgentAwarenessPreferences | null,
39 phase: string,
40): boolean {
41 if (preferences === null) return true;
42 switch (phase) {
43 case "waiting_for_approval":
44 return preferences.notifyOnApproval;
45 case "waiting_for_input":
46 return preferences.notifyOnInput;
47 case "completed":
48 return preferences.notifyOnCompletion;
49 case "failed":
50 return preferences.notifyOnFailure;
51 default:
52 return false;
53 }
55​
56// A missing baseline is a replay, not a transition that should buzz the phone.
57export function attentionTransitionRows(input: TransitionInput) {
58 if (input.previousAggregate === null) return [];
59 const previouslyAttention = new Set(
60 input.previousAggregate.activities.filter((row) => isAttentionPhase(row.phase)).map(rowKey),
61 );
62 return input.nextAggregate.activities.filter(
63 (row) =>
64 isAttentionPhase(row.phase) &&
65 !previouslyAttention.has(rowKey(row)) &&
66 alertAllowedForPhase(input.preferences, row.phase),
67 );
69​
70export function newlyTerminalRows(
71 previousAggregate: RelayAgentActivityAggregateState | null,
72 nextAggregate: RelayAgentActivityAggregateState,
73 includeUnobserved = false,
74): ReadonlyArray<RelayAgentActivityAggregateRow> {
75 if (previousAggregate === null) return [];
76 const previousPhases = new Map(
77 previousAggregate.activities.map((row) => [rowKey(row), row.phase]),
78 );
79 return nextAggregate.activities.filter((row) => {
80 if (row.phase !== "completed" && row.phase !== "failed") return false;
81 const previousPhase = previousPhases.get(rowKey(row));
82 return (
83 (includeUnobserved || previousPhase !== undefined) &&
84 previousPhase !== "completed" &&
85 previousPhase !== "failed"
86 );
87 });
89​
90export function terminalTransitionRows(
91 input: TransitionInput & { readonly nowMs: number; readonly includeUnobserved?: boolean },
92) {
93 return newlyTerminalRows(
94 input.previousAggregate,
95 input.nextAggregate,
96 input.includeUnobserved,
97 ).filter((row) => {
98 return (
99 alertAllowedForPhase(input.preferences, row.phase) &&
100 isFreshTerminalNotification(row.updatedAt, input.nowMs)
101 );
102 });
104​
105export function alertForActivityRows(
106 rows: ReadonlyArray<RelayAgentActivityAggregateRow>,
107): AgentActivityAlert | null {
108 const first = rows[0];
109 if (!first) return null;
110 if (rows.length === 1) {
111 return { title: first.threadTitle, body: `${first.status}: ${first.projectTitle}` };
112 }
113 return {
114 title: `${rows.length} agents ${isAttentionPhase(first.phase) ? "need attention" : "finished"}`,
115 body: rows.map((row) => row.threadTitle).join(", "),
116 };
118​
119export function alertForAttentionTransition(input: TransitionInput): AgentActivityAlert | null {
120 return alertForActivityRows(attentionTransitionRows(input));
122​
123export function alertForNewlyTerminal(
124 input: TransitionInput & { readonly nowMs: number; readonly includeUnobserved?: boolean },
125): AgentActivityAlert | null {
126 return alertForActivityRows(terminalTransitionRows(input));
128​
129export function alertForTerminalAggregate(input: {
130 readonly aggregate: RelayAgentActivityAggregateState | null;
131 readonly preferences: RelayAgentAwarenessPreferences | null;
132}): AgentActivityAlert | null {
133 const row = input.aggregate?.activities[0];
134 if (!row || (row.phase !== "completed" && row.phase !== "failed")) return null;
135 return alertAllowedForPhase(input.preferences, row.phase) ? alertForActivityRows([row]) : null;
137​
138export function shouldAlertForActivity(input: {
139 readonly phase: RelayAgentActivityAggregateRow["phase"];
140 readonly updatedAt: string;
141 readonly preferences: RelayAgentAwarenessPreferences | null;
142 readonly nowMs: number;
143}): boolean {
144 return (
145 input.preferences?.notificationsEnabled === true &&
146 alertAllowedForPhase(input.preferences, input.phase) &&
147 ((input.phase !== "completed" && input.phase !== "failed") ||
148 isFreshTerminalNotification(input.updatedAt, input.nowMs))
149 );