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