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

agentActivityAggregate.ts

130 lines4.4 KB

1import type {
2 RelayAgentActivityAggregateState,
3 RelayAgentActivityState,
4} from "@t3tools/contracts/relay";
5import * as DateTime from "effect/DateTime";
6import * as Option from "effect/Option";
7import {
8 isExpiredAgentActivityState,
9 isTerminalPhase,
10 MAX_ACTIVITY_ROWS,
11 sanitizeAgentActivityAggregateState,
12} from "./agentActivityPayloads.ts";
13​
14export function statusForPhase(phase: RelayAgentActivityState["phase"]): string {
15 switch (phase) {
16 case "waiting_for_approval":
17 return "Approval";
18 case "waiting_for_input":
19 return "Input";
20 case "completed":
21 return "Done";
22 case "failed":
23 return "Failed";
24 case "starting":
25 // Matches the web sidebar's pill wording (Sidebar.logic.ts) so the same
26 // thread reads the same across surfaces.
27 return "Connecting";
28 case "running":
29 return "Working";
30 case "stale":
31 return "Waiting";
32 }
34​
35function aggregateRowForState(state: RelayAgentActivityState) {
36 return {
37 environmentId: state.environmentId,
38 threadId: state.threadId,
39 projectTitle: state.projectTitle,
40 threadTitle: state.threadTitle,
41 modelTitle: state.modelTitle,
42 phase: state.phase,
43 status: statusForPhase(state.phase),
44 updatedAt: state.updatedAt,
45 deepLink: state.deepLink,
46 };
48​
49function terminalAggregateState(state: RelayAgentActivityState): RelayAgentActivityAggregateState {
50 return sanitizeAgentActivityAggregateState({
51 title: "T3 Code",
52 subtitle: state.phase === "failed" ? "Agent work failed" : "Agent work completed",
53 activeCount: 0,
54 updatedAt: state.updatedAt,
55 activities: [aggregateRowForState(state)],
56 });
58​
59export const TERMINAL_AGENT_ACTIVITY_DISPLAY_TTL_MS = 15 * 60 * 1_000;
60​
61function isRecentTerminalState(state: RelayAgentActivityState, nowMs: number): boolean {
62 if (!isTerminalPhase(state)) {
63 return false;
64 }
65 const updatedAtMs = Option.match(DateTime.make(state.updatedAt), {
66 onNone: () => Number.NaN,
67 onSome: (dt) => dt.epochMilliseconds,
68 });
69 if (Number.isNaN(updatedAtMs)) {
70 return false;
71 }
72 return nowMs - updatedAtMs <= TERMINAL_AGENT_ACTIVITY_DISPLAY_TTL_MS;
74​
75export function makeAggregateState(input: {
76 readonly activeStates: ReadonlyArray<RelayAgentActivityState>;
77 readonly terminalState: RelayAgentActivityState | null;
78 readonly nowMs: number;
79}): RelayAgentActivityAggregateState | null {
80 const activeStates = input.activeStates.filter(
81 (state) => !isTerminalPhase(state) && !isExpiredAgentActivityState(state, input.nowMs),
82 );
83 if (activeStates.length === 0) {
84 if (input.terminalState !== null) {
85 return terminalAggregateState(input.terminalState);
86 }
87 // The newly-terminal alert rules key off the previously delivered
88 // aggregate, so replays repaint this without buzzing.
89 const recentTerminal = input.activeStates
90 .filter((state) => isRecentTerminalState(state, input.nowMs))
91 .sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
92 const newest = recentTerminal[0];
93 if (!newest) {
94 return null;
95 }
96 return sanitizeAgentActivityAggregateState({
97 title: "T3 Code",
98 subtitle: newest.phase === "failed" ? "Agent work failed" : "Agent work completed",
99 activeCount: 0,
100 updatedAt: newest.updatedAt,
101 activities: recentTerminal.slice(0, MAX_ACTIVITY_ROWS).map(aggregateRowForState),
102 });
103 }
104 const recentTerminalStates = input.activeStates
105 .filter((state) => isRecentTerminalState(state, input.nowMs))
106 .sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
107 const displayedStates = [
108 ...activeStates
109 .toSorted((a, b) => activityPhasePriority(a.phase) - activityPhasePriority(b.phase))
110 .slice(0, MAX_ACTIVITY_ROWS),
111 ...recentTerminalStates,
112 ].slice(0, MAX_ACTIVITY_ROWS);
113 const updatedAt = [...activeStates, ...recentTerminalStates].reduce((latest, state) =>
114 state.updatedAt.localeCompare(latest.updatedAt) > 0 ? state : latest,
115 ).updatedAt;
116 return sanitizeAgentActivityAggregateState({
117 title: "T3 Code",
118 subtitle: "Agent work in progress",
119 activeCount: activeStates.length,
120 updatedAt,
121 activities: displayedStates.map(aggregateRowForState),
122 });
124​
125export function activityPhasePriority(phase: RelayAgentActivityState["phase"]): number {
126 if (phase === "waiting_for_approval" || phase === "waiting_for_input") return 0;
127 if (phase === "failed") return 1;
128 if (phase === "starting" || phase === "running") return 2;
129 return 3;