mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
41 lines
1022 B
TypeScript
41 lines
1022 B
TypeScript
type ObserverEventSummary = {
|
|
seq: number;
|
|
timestamp: string;
|
|
};
|
|
|
|
type AgentObserverEvents = {
|
|
pubkey: string;
|
|
events: readonly ObserverEventSummary[];
|
|
};
|
|
|
|
function latestEventKey(
|
|
events: readonly ObserverEventSummary[],
|
|
): string | null {
|
|
const latest = events.length > 0 ? events[events.length - 1] : undefined;
|
|
return latest ? `${latest.timestamp}:${latest.seq}` : null;
|
|
}
|
|
|
|
export function createPreventSleepActivityTracker() {
|
|
const lastEventByAgent = new Map<string, string>();
|
|
|
|
return {
|
|
observe(agents: readonly AgentObserverEvents[]): boolean {
|
|
let hasNewActivity = false;
|
|
|
|
for (const agent of agents) {
|
|
const latestKey = latestEventKey(agent.events);
|
|
if (!latestKey) continue;
|
|
|
|
const previousKey = lastEventByAgent.get(agent.pubkey);
|
|
lastEventByAgent.set(agent.pubkey, latestKey);
|
|
|
|
if (previousKey !== undefined && previousKey !== latestKey) {
|
|
hasNewActivity = true;
|
|
}
|
|
}
|
|
|
|
return hasNewActivity;
|
|
},
|
|
};
|
|
}
|